是否可以在没有轴的情况下向绘图中添加另一个值? [英] Is it possible to add another value to the plotly plot without the axis?

查看:94
本文介绍了是否可以在没有轴的情况下向绘图中添加另一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我需要绘制带有两个轴xy的可绘图形.是否可以在该图中添加另一个参数(例如z)而无需在图中绘制它?

I have a requirement, where I need to plot a plotly graph with two axes x and y. Is it possible to add another parameter to that graph (say z) without plotting it in the graph?

例如,如果水在凌晨5点在100 o C沸腾,我想以X绘制时间,以Y绘制度数,并在出现时添加水"一词悬停在该点上.

For example, if water boils at 100 oC at time 5 AM, I want to plot time in X and degrees at Y and the word 'Water' to be added when hovering over that point.

更新: 在下图中,将数据帧悬停在该点上时显示了两个点,现在我想知道是否有可能将数据帧的另一列添加到悬停,即对应于16.3、621.1的列为300,那么我也想在悬停中显示300,而无需明确绘制它.

UPDATE: In the below image, there are two points of a dataframe being shown when hovered about that point, now I want to know if its possible to add another column of the dataframe to the hover i.e, the column corresponding to 16.3, 621.1 is 300, then I want to show 300 in the hover too, without explicitly plotting it.

谢谢

Shyam

推荐答案

这绝对不是一个优雅的解决方案,但是它可以工作:

This is definitely not an elegant solution, but it works:

import plotly
import plotly.graph_objs as go
import pandas as pd

#Create a pandas DataFrame
df = pd.DataFrame({"temp":["100", "15", "95", "90", "85"],
                   "time":["4 AM", "5 AM", "6 AM", "7 AM", "8 AM"],
                   "substance":["Water", "Milk", "Honey", "Beer", "Soda"]})
#Create a lists from DataFrame columns
temp = df["temp"]
time = df["time"]
substance = df["substance"]
#Create an empty list
textlist = []
#Fill this list with info from all of the lists above
for i in [*range(len(temp))]:
    i = temp[i] + "," + time[i] + "," + substance[i] 
    textlist.append(i)
#Set title plot
title = "Boil water"
#Choose in parameter text what you want to see (textlist)
data = [go.Scatter(x = df["time"], 
                   y = df["temp"], 
                   text = textlist,
                   hoverinfo = "text",
                   marker = dict(color = "green"),
                   showlegend = False)]
#Using plotly in offline mode
plotly.offline.init_notebook_mode(connected=True)  
#Save plot in directory where your script located without open in browser
plotly.offline.plot({"data": data, "layout": go.Layout(title=title)},
                    auto_open=False, filename = str(title) + ".html")

时间在X轴上,温度在Y轴上,当您将鼠标悬停在图表中的任何点上时,您都会看到类似"100,4 AM,Water"的信息

Time on Xaxis, temperature on Yaxis and when you hover onto any of points in graph, you will see something like "100, 4 AM, Water"

这篇关于是否可以在没有轴的情况下向绘图中添加另一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆