Python 我们如何在Plotly中的子地块中制作镶嵌面网格?

Python 我们如何在Plotly中的子地块中制作镶嵌面网格?,python,plotly,subplot,facet-grid,Python,Plotly,Subplot,Facet Grid,我们如何在Plotly中的子地块中制作镶嵌面网格?例如,我想将总账单与小费在子批次中绘制五次。我厌倦了做以下事情: 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.gith

我们如何在Plotly中的子地块中制作镶嵌面网格?例如,我想将
总账单
小费
在子批次中绘制五次。我厌倦了做以下事情:

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)

这不起作用,因为地物工厂创建的镶嵌面栅格不被视为跟踪。有没有办法做到这一点?答案没有帮助,因为在我看来,
袖扣
不接受平面网格

这里发生了很多事情

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

    for i in range(tips.columns.size):
    
    否则,您可以通过执行

    for i, col in enumerate(tips.columns):
    
  • figure factory中的方法返回
    ,其中包含
    跟踪
    ,在其
    数据
    列表中。您可以通过索引访问
    create\u facet\u grid
    方法生成的一条记录道:

    subfig.append_trace(fig['data'][index_of_the_trace], n1, n2)
    
  • 分面网格的思想是通过数据集的一个分类列分割数据集,下面是一个示例,说明如何通过某些选定列将数据集的分区分配给不同的子地块:

    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)
    
  • 希望能有帮助