Plotly:如何在同一个 pandas 数据帧的不同列中的一个 plotly 图表中绘制多条线?
Plotly: How to plot multiple lines in one plotly chart from different columns from the same pandas dataframe?
我有以下 table 作为 pandas:
>>>date hour plant1 plant2 plant3 plant4 ...
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 df[wide_variable_0]
is 2814, whereas the length of
previously-processed arguments ['x'] is 201
我知道 for plotly 不理解我输入的 iloc 来单独绘制每一列。
我如何告诉它把每一列绘制成单独的线(例如像这样但是用我的数据和每一列的线,所以我们将有列名而不是国家):
*这个例子来自 plotly manual (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:])
它可以工作,但不是交互式的。
您可以简单地 使用 to_plot = [v for v in list(df.columns) if v.startswith('plant')]
在 list(df.columns)
中绘制,然后使用 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()
您能提供一部分数据吗?
我不知道你到底用了什么作为x。 df.iloc[:,2]
看起来像 plant1
一般来说,新版本的 plotly 可能需要多个 y,旧版本可能不会;如果更新包仍然不起作用,请像这样合并数据框:
list = # all the lines you want to draw, eg ['plant1','plant2']
df = pd.melt(df,
id_vars=["date", "hour"],
value_vars= list ,
var_name="plant_number",
value_name="y")
fig = px.line(df, x= "date", y="y" ,color = "plant_number")
fig.show()
我有以下 table 作为 pandas:
>>>date hour plant1 plant2 plant3 plant4 ...
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
df[wide_variable_0]
is 2814, whereas the length of previously-processed arguments ['x'] is 201
我知道 for plotly 不理解我输入的 iloc 来单独绘制每一列。
我如何告诉它把每一列绘制成单独的线(例如像这样但是用我的数据和每一列的线,所以我们将有列名而不是国家):
*这个例子来自 plotly manual (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:])
它可以工作,但不是交互式的。
您可以简单地 to_plot = [v for v in list(df.columns) if v.startswith('plant')]
在 list(df.columns)
中绘制,然后使用 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()
您能提供一部分数据吗?
我不知道你到底用了什么作为x。 df.iloc[:,2]
看起来像 plant1
一般来说,新版本的 plotly 可能需要多个 y,旧版本可能不会;如果更新包仍然不起作用,请像这样合并数据框:
list = # all the lines you want to draw, eg ['plant1','plant2']
df = pd.melt(df,
id_vars=["date", "hour"],
value_vars= list ,
var_name="plant_number",
value_name="y")
fig = px.line(df, x= "date", y="y" ,color = "plant_number")
fig.show()