Plotly:如何从同一pandas数据框的不同列中在一个Plotly图表中绘制多条线?
Plotly:如何从同一pandas数据框的不同列中在一个Plotly图表中绘制多条线?
我有以下表格作为pandas数据框:
>>>日期 小时 工厂1 工厂2 工厂3 工厂4 ... 0 2019-06-23 07:00:00 251.2 232.7 145.1 176.7 1 2019-06-23 07:02:00 123.4 173.1 121.5 180.4 2 2019-06-23 07:04:00 240.1 162.7 140.1 199.5 3 2019-06-23 07:06:00 224.8 196.5 134.1 200.5 4 2019-06-23 07:08:00 124.3 185.4 132.3 190.1 ...
我想要交互地绘制每个工厂(每一列)的折线图,包括所有的工厂列。
只用plotly绘制一条线对我来说是可以的:
import plotly.express as px fig = px.line(df, x=df.iloc[:,2], y=df.iloc[:,3]) fig.show()
但是当我尝试使用iloc绘制所有列时,像这样放置所有列时,它失败了:
fig = px.line(df, x=df.iloc[:,2], y=df.iloc[:,3:])
报错信息如下:
ValueError: All arguments should have the same length. The length of column argument [apcode]df[wide_variable_0]is 2814, whereas the length of
previously-processed arguments ['x'] is 201
[/apcode]
我明白plotly无法理解我用iloc输入以单独绘制每一列的方式。
我该如何告诉它以每列作为单独的线绘制(例如,类似于这个示例,但使用我的数据,每列都有一条线,所以代替国家名字,我们将有列名):
[a href="https://i.stack.imgur.com/NFJce.png" rel="nofollow noreferrer">
*此示例来自plotly手册(https://plotly.com/python/line-charts/)
我的最终目标:为每个工厂列绘制一条线
编辑:我也尝试了使用pandas的方法,如此处描述,但是出现错误:
dfs['2019-06-23'].iloc[:,2:].plot(kind='line') >>> ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected.
但是当我"更改顺序"时:
plt.plot(df.iloc[:,2:])
它可以工作,但是不是交互式的。
问题的出现原因:在使用Plotly绘制图表时,需要将来自同一数据框的不同列的数据绘制在同一个图表中的多条线。然而,旧版本的Plotly可能不支持这种功能。
解决方法:首先,可以尝试更新Plotly的版本,因为较新的版本可能已经支持绘制多条线。如果更新后仍无法实现,可以使用Pandas的melt函数将数据框中的列合并,然后再绘制图表。
具体的解决方法如下所示:
1. 首先,提供一部分数据用于示例。
2. 确定x轴数据,例如使用df.iloc[:,2]作为x轴数据。
3. 使用Pandas的melt函数合并数据框的列,将要绘制的所有线的数据合并到一列中。
4. 使用Plotly的px.line函数绘制图表,指定x轴数据和y轴数据,并使用color参数指定不同的线。
5. 使用fig.show()显示图表。
以下是代码示例:
# 提供一部分数据 # could you provide a slice of your data? df = pd.DataFrame({"date": ["2022-01-01", "2022-01-02", "2022-01-03"], "hour": [1, 2, 3], "plant1": [10, 20, 30], "plant2": [15, 25, 35]}) # 确定x轴数据 # I don't know what exactly you used as x. df.iloc[:,2] looks like plant1 x = df.iloc[:, 2] # 使用Pandas的melt函数合并数据框的列 # merge the dataframe like this: list = ['plant1', 'plant2'] df = pd.melt(df, id_vars=["date", "hour"], value_vars=list, var_name="plant_number", value_name="y") # 使用Plotly的px.line函数绘制图表 fig = px.line(df, x="date", y="y", color="plant_number") # 显示图表 fig.show()
通过以上步骤,我们可以将来自同一数据框的不同列的数据绘制在同一个图表中的多条线。
在这个问题中,用户想要在同一个Plotly图表中绘制来自同一pandas数据框的不同列的多条线。用户已经尝试了一些方法,但没有成功。用户希望找到一种方法来解决这个问题,并且还希望有一个良好的资源来学习Plotly Express。
用户得到了一个答案,解释了如何使用Plotly Express绘制多条线。首先,用户需要定义他们想要绘制的列名,可以使用list(df.columns)
来获取列名列表。然后,用户可以使用列表推导式to_plot = [v for v in list(df.columns) if v.startswith('plant')]
来选择以"plant"开头的列名。最后,用户可以使用fig = px.line(df, x=df.index, y=to_plot)
来绘制图表。
下面是完整的代码:
import pandas as pd import plotly.express as px df = pd.DataFrame({'date': {0: '2019-06-23', 1: '2019-06-23', 2: '2019-06-23', 3: '2019-06-23', 4: '2019-06-23'}, 'hour': {0: '07:00:00', 1: '07:02:00', 2: '07:04:00', 3: '07:06:00', 4: '07:08:00'}, 'plant1': {0: 251.2, 1: 123.4, 2: 240.1, 3: 224.8, 4: 124.3}, 'plant2': {0: 232.7, 1: 173.1, 2: 162.7, 3: 196.5, 4: 185.4}, 'plant3': {0: 145.1, 1: 121.5, 2: 140.1, 3: 134.1, 4: 132.3}, 'plant4': {0: 176.7, 1: 180.4, 2: 199.5, 3: 200.5, 4: 190.1}}) df['ix'] = df['date']+' ' +df['hour'] df['ix'] = pd.to_datetime(df['ix']) to_plot = [v for v in list(df.columns) if v.startswith('plant')] fig = px.line(df, x=df.index, y=to_plot) fig.show()
这个答案解决了用户的问题,并提供了用户所需的预期结果。
最后,用户还提到了对于Plotly Express的学习资源的需求,希望有一个良好的来源来学习Plotly Express。但是,这个问题并没有给出关于学习资源的答案。