首页 文章

是否可以在没有轴的情节图中添加另一个值?

提问于
浏览
2

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

例如,如果水在时间凌晨5点沸腾到100 oC,我想在 X 中绘制时间,在 Y 处绘制度数,并在悬停在该点上时添加单词'Water' .

更新:在下面的图像中,当围绕该点悬停时,有两个点显示数据帧,现在我想知道是否可以将数据帧的另一列添加到悬停,即对应于16.3,621.1的列是300,然后我想在悬停中显示300,而没有明确地绘制它 .
enter image description here

谢谢,

希亚姆

1 回答

  • 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轴上的时间,在Yaxis上的温度以及当您将鼠标悬停在图表中的任何点上时,您将看到类似“100,4 AM,Water”的内容

相关问题