python - 是否可以在没有轴的情况下向绘图添加另一个值?

标签 python jupyter-notebook plotly jupyter

我有一个要求,我需要绘制一个带有两个轴xy的绘图。是否可以向该图表添加另一个参数(例如 z)而不将其绘制在图表中?

例如,如果水在上午 5 点的温度为 100 oC,我想以 X 为单位绘制时间,以 Y 为单位绘制度数并将鼠标悬停在该点上时添加“水”一词。

更新: 在下图中,当鼠标悬停在该点附近时,会显示数据帧的两个点,现在我想知道是否可以将数据帧的另一列添加到悬停中,即与 16.3、621.1 对应的列是 300,然后我也想在悬停中显示 300,而不明确绘制它。 enter image description here

谢谢

夏姆

最佳答案

这绝对不是一个优雅的解决方案,但它有效:

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”的内容

关于python - 是否可以在没有轴的情况下向绘图添加另一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51766989/

相关文章:

python - 寻找多个县之间最大的人口差异?

python - Django:将计算应用于查询集

python - 行号和语法突出显示在不同选项卡上不起作用

python - Plotly:当 (n) 个点被绘制时,标记消失

python - 避免在 scikit learn StandardScaler 中缩放二进制列

python - Jupyter Notebook 执行在 Visual Studio 代码中变灰

jupyter-notebook - 在 Jupyter Notebook 上使用 Google Colab 的用例?

python - matplotlib 如何在不在 jupyter 笔记本中时显示绘图

python - 为什么 plotly.offline.iplot( {'x' : [1, 2, 3], 'y' : [5, 2, 7]}) 不起作用?

python - Plotly:始终显示悬停文本