【问题标题】:How can we make a faceted grid in subplots in Plotly?我们如何在 Plotly 的子图中制作多面网格?
【发布时间】:2018-07-25 14:46:40
【问题描述】:

我们如何在 Plotly 的子图中制作多面网格?例如,我想在子图中将total_billtip 绘制五次。我厌倦了以下操作:

import plotly.plotly as py
import plotly.figure_factory as ff
from plotly import tools

subfigs = tools.make_subplots(rows= 5, cols=1)

import pandas as pd
tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')

for i in enumerate(tips.columns.tolist()):
    fig = ff.create_facet_grid(
    tips,
    x='total_bill',
    y='tip',
    color_name='sex',
    show_boxes=False,
    marker={'size': 10, 'opacity': 1.0},
    colormap={'Male': 'rgb(165, 242, 242)', 'Female': 'rgb(253, 174, 216)'}
    )

    subfigs.append_trace(fig, i+1, 1)

pyo.iplot(fig)

这不起作用,因为图形工厂创建的多面网格不被视为跟踪。有没有办法做到这一点? This 回答没有帮助,因为在我看来 cufflinks 不接受多面网格

【问题讨论】:

    标签: python plotly subplot facet-grid


    【解决方案1】:

    这里发生了很多事情。

    1. python 中的函数enumerate 为您提供一个元组列表,如果您只想通过可以使用的索引进行迭代

      for i in range(tips.columns.size):
      

      否则你可以通过做解压

      for i, col in enumerate(tips.columns):
      
    2. 图形工厂的方法返回Figures,它的data列表中包含Traces。您可以通过其索引访问create_facet_grid 方法产生的跟踪之一:

      subfig.append_trace(fig['data'][index_of_the_trace], n1, n2)
      
    3. 分面网格的想法是通过其中一个分类列拆分数据集,这里是一个示例,说明如何通过某些选定列将数据集的分区分配给不同的子图:

      tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')
      
      current_column = 'sex'
      
      subfigs = tools.make_subplots(
          rows = tips[current_column].nunique(),
          cols = 1
      )
      
      fig = ff.create_facet_grid(
          tips,
          x = 'total_bill',
          y = 'tip',
          facet_row  = current_column,
          color_name = 'sex',
          show_boxes = False,
          marker     = {'size': 10, 'opacity': 1.0},
          colormap   = {
              'Male': 'rgb(165, 242, 242)',
              'Female': 'rgb(253, 174, 216)'
          }
      )
      
      for i in range(tips[current_column].nunique()):
          subfigs.append_trace(fig['data'][i], i+1, 1)
      
      py.iplot(fig)
      

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2021-08-27
      • 2020-07-14
      • 2012-09-22
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      相关资源
      最近更新 更多