Is it possible to add another value to the plotly

2019-07-14 06:27发布

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?

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.

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. enter image description here

Thanks,

Shyam

1条回答
手持菜刀,她持情操
2楼-- · 2019-07-14 06:57

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")

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"

查看更多
登录 后发表回答