有没有办法使用 Plotly express 来显示多个子图
Is there a way to use Plotly express to show multiple subplots
我很想知道是否有等同于:
import pandas as pd
import numpy as np
data = pd.DataFrame({'Day':range(10),
'Temperature': np.random.rand(10),
'Wind': np.random.rand(10),
'Humidity': np.random.rand(10),
'Pressure': np.random.rand(10)})
data.set_index('Day').plot(subplots=True, layout=(2,2), figsize=(10,5))
plt.tight_layout()
生成 Plotly 图表而不是 matplotlib 图表。
- 根据文档,Plotly Express does not support arbitrary subplot capabilities, instead it supports faceting by a given data dimension, and it also supports marginal charts to display distribution information。
- 这演示了较低级别
plotly.subplots
模块的用法和它公开的 make_subplots
函数,用于构建具有任意子图的图形。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# using your sample data
fig = make_subplots(rows=2, cols=2, start_cell="bottom-left")
fig.add_trace(go.Scatter(x=data.index, y=data.Temperature, name='Temp'),
row=1, col=1, )
fig.add_trace(go.Scatter(x=data.index, y=data.Wind, name='Wind'),
row=1, col=2)
fig.add_trace(go.Scatter(x=data.index, y=data.Humidity, name='Humidity'),
row=2, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data.Pressure, name='Pressure'),
row=2, col=2)
fig.show()
对于一个 plotly express 解决方案:
您可以使用 pd.melt()
在同一列中获取所有变量:
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
'Day':range(10),
'Temperature': np.random.rand(10),
'Wind': np.random.rand(10),
'Humidity': np.random.rand(10),
'Pressure': np.random.rand(10),})
df_melt = df.melt(
id_vars='Day',
value_vars=['Temperature', 'Wind', 'Humidity', 'Pressure'])
您的数据框现在看起来像这样,变量名称在名为 'variable' 的列中,值在名为 'value':
的列中
Day variable value
0 0 Temperature 0.609
1 1 Temperature 0.410
2 2 Temperature 0.194
3 3 Temperature 0.663
4 4 Temperature 0.351
现在您可以使用带有参数 facet_col
的 px.scatter()
来获取多个图:
fig = px.scatter(
df_melt,
x='Day',
y='value',
facet_col='variable',
facet_col_wrap=2,
color='variable',
width=800,
)
结果如下图:
现在在您的示例中,所有变量都具有相同的值范围。但如果不是这种情况,那么您可能需要确保每个绘图在 y 轴上都有自己的范围。这可以按如下方式完成:
fig.update_yaxes(showticklabels=True, matches=None)
可以在此处找到有关分面图的更多信息:
https://plotly.com/python/facet-plots/
我只想快速绘制多个分布子图,就像在 sns 中一样,pyplot。对于循环工作。当然也适用于分散。不错:连 xlables 都打印出来了。
for col in boston_df.columns.tolist():
boston_dis = px.histogram(boston_df,
x=col, color_discrete_sequence=['lavenderblush'],
title='Distribution',
histnorm='probability density', template='plotly_dark',
width=400, height=300)
boston_dis.show()
我很想知道是否有等同于:
import pandas as pd
import numpy as np
data = pd.DataFrame({'Day':range(10),
'Temperature': np.random.rand(10),
'Wind': np.random.rand(10),
'Humidity': np.random.rand(10),
'Pressure': np.random.rand(10)})
data.set_index('Day').plot(subplots=True, layout=(2,2), figsize=(10,5))
plt.tight_layout()
生成 Plotly 图表而不是 matplotlib 图表。
- 根据文档,Plotly Express does not support arbitrary subplot capabilities, instead it supports faceting by a given data dimension, and it also supports marginal charts to display distribution information。
- 这演示了较低级别
plotly.subplots
模块的用法和它公开的make_subplots
函数,用于构建具有任意子图的图形。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# using your sample data
fig = make_subplots(rows=2, cols=2, start_cell="bottom-left")
fig.add_trace(go.Scatter(x=data.index, y=data.Temperature, name='Temp'),
row=1, col=1, )
fig.add_trace(go.Scatter(x=data.index, y=data.Wind, name='Wind'),
row=1, col=2)
fig.add_trace(go.Scatter(x=data.index, y=data.Humidity, name='Humidity'),
row=2, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data.Pressure, name='Pressure'),
row=2, col=2)
fig.show()
对于一个 plotly express 解决方案:
您可以使用 pd.melt()
在同一列中获取所有变量:
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
'Day':range(10),
'Temperature': np.random.rand(10),
'Wind': np.random.rand(10),
'Humidity': np.random.rand(10),
'Pressure': np.random.rand(10),})
df_melt = df.melt(
id_vars='Day',
value_vars=['Temperature', 'Wind', 'Humidity', 'Pressure'])
您的数据框现在看起来像这样,变量名称在名为 'variable' 的列中,值在名为 'value':
的列中 Day variable value
0 0 Temperature 0.609
1 1 Temperature 0.410
2 2 Temperature 0.194
3 3 Temperature 0.663
4 4 Temperature 0.351
现在您可以使用带有参数 facet_col
的 px.scatter()
来获取多个图:
fig = px.scatter(
df_melt,
x='Day',
y='value',
facet_col='variable',
facet_col_wrap=2,
color='variable',
width=800,
)
结果如下图:
现在在您的示例中,所有变量都具有相同的值范围。但如果不是这种情况,那么您可能需要确保每个绘图在 y 轴上都有自己的范围。这可以按如下方式完成:
fig.update_yaxes(showticklabels=True, matches=None)
可以在此处找到有关分面图的更多信息:
https://plotly.com/python/facet-plots/
我只想快速绘制多个分布子图,就像在 sns 中一样,pyplot。对于循环工作。当然也适用于分散。不错:连 xlables 都打印出来了。
for col in boston_df.columns.tolist():
boston_dis = px.histogram(boston_df,
x=col, color_discrete_sequence=['lavenderblush'],
title='Distribution',
histnorm='probability density', template='plotly_dark',
width=400, height=300)
boston_dis.show()