我有一个要求,我需要绘制带有两个轴x
和y
的图形。是否可以在该图中添加另一个参数(例如z
)而不在图中绘制它?
例如,如果水在凌晨5点在100 o C沸腾,我想以X
绘制时间,以Y
绘制度数,并标出“ Water”一词悬停在该点时添加。
更新: 在下图中,将数据帧悬停在该点上时显示了两个点,现在我想知道是否有可能将数据帧的另一列添加到悬停,即对应于16.3、621.1的列为300,那么我也想在悬停中显示300,而无需明确绘制它。
谢谢
Shyam
答案 0 :(得分:2)
这绝对不是一个优雅的解决方案,但是它可以工作:
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”的信息