【问题标题】:Is there a way to use Plotly express to show multiple subplots有没有办法使用 Plotly express 显示多个子图
【发布时间】:2021-04-18 02:53:51
【问题描述】:

我很想知道是否有相当于:

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 图表。

【问题讨论】:

    标签: python pandas plotly plotly-python plotly-express


    【解决方案1】:

    我只想像 sns、pyplot 一样快速地一个接一个地绘制多个分布子图。 For循环有效。当然也适用于分散。手感不错:甚至 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()
    

    【讨论】:

      【解决方案2】:

      对于一个巧妙的解决方案:
      您可以使用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'])
      

      您的数据框现在看起来像这样,变量名称位于名为“变量”的列中,值位于名为“值”的列中:

          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
      

      现在您可以使用 px.scatter() 和参数 facet_col 来获取多个图:

      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/

      【讨论】:

        【解决方案3】:
        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()
        

        【讨论】:

          猜你喜欢
          • 2013-02-10
          • 1970-01-01
          • 2022-08-09
          • 2022-07-07
          • 1970-01-01
          • 1970-01-01
          • 2016-02-12
          • 1970-01-01
          • 2020-02-09
          相关资源
          最近更新 更多