我们如何在 Plotly 的子图中制作多面网格?
How can we make a faceted grid in subplots in Plotly?
我们如何在 Plotly 的子图中制作多面网格?例如,我想在子图中绘制 total_bill
与 tip
五次。我厌倦了做以下事情:
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)
这不起作用,因为图形工厂创建的多面网格不被视为跟踪。有没有办法做到这一点? 答案没有帮助,因为在我看来 cufflinks
不接受多面网格
这里发生了很多事情。
python 中的函数 enumerate
给你一个元组列表,如果你只想通过索引迭代你可以使用
for i in range(tips.columns.size):
否则你可以通过
解压
for i, col in enumerate(tips.columns):
图形工厂 return Figures
中的方法,其 data
列表中包含 Traces
。您可以通过其索引访问 create_facet_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)
希望对您有所帮助。
我们如何在 Plotly 的子图中制作多面网格?例如,我想在子图中绘制 total_bill
与 tip
五次。我厌倦了做以下事情:
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)
这不起作用,因为图形工厂创建的多面网格不被视为跟踪。有没有办法做到这一点? cufflinks
不接受多面网格
这里发生了很多事情。
python 中的函数
enumerate
给你一个元组列表,如果你只想通过索引迭代你可以使用for i in range(tips.columns.size):
否则你可以通过
解压for i, col in enumerate(tips.columns):
图形工厂 return
Figures
中的方法,其data
列表中包含Traces
。您可以通过其索引访问create_facet_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)
希望对您有所帮助。