如何使用 Plotly in Python 将饼图绘制为具有自定义大小的子图
How to plot pie charts as subplots with custom size with Plotly in Python
我一直在尝试在 Jupyter notebook(离线)中使用 Plotly(版本 1.12.9)制作具有自定义大小的子图网格。 Plotly 网站上有很好的例子,但它们都是分散的情节。我修改了其中一个,使其看起来像我想要的那个,并且它适用于散点图:
import plotly
import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode(connected=True)
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
trace0 = go.Scatter(x=[1, 2], y=[1, 2])
trace1 = go.Scatter(x=[1, 2], y=[1, 2])
trace2 = go.Scatter(x=[1, 2], y=[1, 2])
trace3 = go.Scatter(x=[1, 2], y=[1, 2])
trace4 = go.Scatter(x=[1, 2], y=[1, 2])
trace5 = go.Scatter(x=[1, 2], y=[1, 2])
fig = plotly.tools.make_subplots(
rows=3,
cols=3,
specs=[[{}, {}, {}], [{}, {'colspan': 2, 'rowspan': 2}, None], [{} , None, None]],
subplot_titles=('First Subplot','Second Subplot', 'Third Subplot')
)
fig.append_trace(trace0, 3, 1)
fig.append_trace(trace1, 2, 1)
fig.append_trace(trace2, 1, 1)
fig.append_trace(trace3, 1, 2)
fig.append_trace(trace4, 1, 3)
fig.append_trace(trace5, 2, 2)
py.iplot(fig)
并按预期工作:
但像这样更改饼图的轨迹:
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
trace0 = go.Pie(labels=labels,values=values)
trace1 = go.Pie(labels=labels,values=values)
trace2 = go.Pie(labels=labels,values=values)
trace3 = go.Pie(labels=labels,values=values)
trace4 = go.Pie(labels=labels,values=values)
trace5 = go.Pie(labels=labels,values=values)
只是抛出这个错误:
PlotlyDictKeyError: 'xaxis' is not allowed in 'pie'
Path To Error: ['xaxis']
Valid attributes for 'pie' at path [] under parents []:
['pullsrc', 'textfont', 'hoverinfo', 'domain', 'label0', 'legendgroup',
'showlegend', 'scalegroup', 'textpositionsrc', 'pull', 'visible',
'sort', 'name', 'outsidetextfont', 'dlabel', 'stream', 'hole',
'textinfo', 'marker', 'labels', 'labelssrc', 'rotation', 'opacity',
'values', 'insidetextfont', 'direction', 'textsrc', 'textposition',
'type', 'valuessrc', 'text', 'uid']
Run `<pie-object>.help('attribute')` on any of the above.
'<pie-object>' is the object at []
只有分散的地块才能做到这一点吗?我在 plotly 文档中没有找到任何内容。
我最近遇到了同样的问题,但没有发现我们是否可以将 plotly.tools.make_subplots
与 plotly.graph_objs.Pie
一起使用。据我所知,这是不可能的,因为这些图没有 x 和 y 轴。在original tutorial for Pie
, they do subplots with providing a domain
dict, e.g. {'x': [0.0, 0.5], 'y': [0.0, 0.5]}
defines an area in the bottom left quadrant of the total plotting space. Btw, this tutorial witholds the solution for annotation positioning at donut charts, what can be done with providing xanchor = 'center'
and yanchor = 'middle'
parameters. I found one other tutorial里面给出了一个很好的例子。在这里我用你的例子来展示它:
import plotly
import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode(connected=True)
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
domains = [
{'x': [0.0, 0.33], 'y': [0.0, 0.33]},
{'x': [0.0, 0.33], 'y': [0.33, 0.66]},
{'x': [0.0, 0.33], 'y': [0.66, 1.0]},
{'x': [0.33, 0.66], 'y': [0.0, 0.33]},
{'x': [0.66, 1.0], 'y': [0.0, 0.33]},
{'x': [0.33, 1.0], 'y': [0.33, 1.0]}
]
traces = []
for domain in domains:
trace = go.Pie(labels = labels,
values = values,
domain = domain,
hoverinfo = 'label+percent+name')
traces.append(trace)
layout = go.Layout(height = 600,
width = 600,
autosize = False,
title = 'Main title')
fig = go.Figure(data = traces, layout = layout)
py.iplot(fig, show_link = False)
p.s。抱歉,后来我意识到 y 坐标是从底部开始的,所以我垂直镜像了您的布局。此外,您可能希望在相邻的子图之间添加 space(只需在布局中稍微给出 smaller/greater 数字,例如,右侧为 0.31 而不是 0.33,左角为 0.35 而不是 0.33)。
这样的批评者
我一直在尝试在 Jupyter notebook(离线)中使用 Plotly(版本 1.12.9)制作具有自定义大小的子图网格。 Plotly 网站上有很好的例子,但它们都是分散的情节。我修改了其中一个,使其看起来像我想要的那个,并且它适用于散点图:
import plotly
import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode(connected=True)
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
trace0 = go.Scatter(x=[1, 2], y=[1, 2])
trace1 = go.Scatter(x=[1, 2], y=[1, 2])
trace2 = go.Scatter(x=[1, 2], y=[1, 2])
trace3 = go.Scatter(x=[1, 2], y=[1, 2])
trace4 = go.Scatter(x=[1, 2], y=[1, 2])
trace5 = go.Scatter(x=[1, 2], y=[1, 2])
fig = plotly.tools.make_subplots(
rows=3,
cols=3,
specs=[[{}, {}, {}], [{}, {'colspan': 2, 'rowspan': 2}, None], [{} , None, None]],
subplot_titles=('First Subplot','Second Subplot', 'Third Subplot')
)
fig.append_trace(trace0, 3, 1)
fig.append_trace(trace1, 2, 1)
fig.append_trace(trace2, 1, 1)
fig.append_trace(trace3, 1, 2)
fig.append_trace(trace4, 1, 3)
fig.append_trace(trace5, 2, 2)
py.iplot(fig)
并按预期工作:
但像这样更改饼图的轨迹:
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
trace0 = go.Pie(labels=labels,values=values)
trace1 = go.Pie(labels=labels,values=values)
trace2 = go.Pie(labels=labels,values=values)
trace3 = go.Pie(labels=labels,values=values)
trace4 = go.Pie(labels=labels,values=values)
trace5 = go.Pie(labels=labels,values=values)
只是抛出这个错误:
PlotlyDictKeyError: 'xaxis' is not allowed in 'pie'
Path To Error: ['xaxis']
Valid attributes for 'pie' at path [] under parents []:
['pullsrc', 'textfont', 'hoverinfo', 'domain', 'label0', 'legendgroup',
'showlegend', 'scalegroup', 'textpositionsrc', 'pull', 'visible',
'sort', 'name', 'outsidetextfont', 'dlabel', 'stream', 'hole',
'textinfo', 'marker', 'labels', 'labelssrc', 'rotation', 'opacity',
'values', 'insidetextfont', 'direction', 'textsrc', 'textposition',
'type', 'valuessrc', 'text', 'uid']
Run `<pie-object>.help('attribute')` on any of the above.
'<pie-object>' is the object at []
只有分散的地块才能做到这一点吗?我在 plotly 文档中没有找到任何内容。
我最近遇到了同样的问题,但没有发现我们是否可以将 plotly.tools.make_subplots
与 plotly.graph_objs.Pie
一起使用。据我所知,这是不可能的,因为这些图没有 x 和 y 轴。在original tutorial for Pie
, they do subplots with providing a domain
dict, e.g. {'x': [0.0, 0.5], 'y': [0.0, 0.5]}
defines an area in the bottom left quadrant of the total plotting space. Btw, this tutorial witholds the solution for annotation positioning at donut charts, what can be done with providing xanchor = 'center'
and yanchor = 'middle'
parameters. I found one other tutorial里面给出了一个很好的例子。在这里我用你的例子来展示它:
import plotly
import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode(connected=True)
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
domains = [
{'x': [0.0, 0.33], 'y': [0.0, 0.33]},
{'x': [0.0, 0.33], 'y': [0.33, 0.66]},
{'x': [0.0, 0.33], 'y': [0.66, 1.0]},
{'x': [0.33, 0.66], 'y': [0.0, 0.33]},
{'x': [0.66, 1.0], 'y': [0.0, 0.33]},
{'x': [0.33, 1.0], 'y': [0.33, 1.0]}
]
traces = []
for domain in domains:
trace = go.Pie(labels = labels,
values = values,
domain = domain,
hoverinfo = 'label+percent+name')
traces.append(trace)
layout = go.Layout(height = 600,
width = 600,
autosize = False,
title = 'Main title')
fig = go.Figure(data = traces, layout = layout)
py.iplot(fig, show_link = False)
p.s。抱歉,后来我意识到 y 坐标是从底部开始的,所以我垂直镜像了您的布局。此外,您可能希望在相邻的子图之间添加 space(只需在布局中稍微给出 smaller/greater 数字,例如,右侧为 0.31 而不是 0.33,左角为 0.35 而不是 0.33)。
这样的批评者