如何在Python中使用Plotly将饼图绘制为具有自定义大小的子图

问题描述:

我一直在尝试使用Jupyter笔记本(离线版)中的Plotly(版本1.12.9)制作具有自定义尺寸的子图网格.在Plotly网站上有很好的示例,但所有示例都带有分散的地块.我修改了其中一个,使其看起来像我想要的,并且可以与散点图一起使用:

I've been trying to make a grid of subplots with custom size with Plotly(version 1.12.9) in Jupyter notebook(offline). There is nice examples in the Plotly website but all of them are with scattered plots. I modified one of them to make it look like the one I want to and it works with scatter plots:

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)

并按预期工作:

但是要更改饼图的轨迹,如下所示:

But changing the traces for pie charts like this:

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 []

只能在散布的地块上这样做吗?在可打印的文档中没有找到任何内容.

Is only possible to do this with scattered plots? I didn't find anything in the plotly documentation.

我最近也遇到了同样的问题,但是对于我们是否可以将plotly.tools.make_subplotsplotly.graph_objs.Pie结合使用一无所获.据我了解,这是不可能的,因为这些图没有x和y轴.在Pie原始教程中,他们通过提供domain字典来做子图,例如{'x': [0.0, 0.5], 'y': [0.0, 0.5]}在总绘图空间的左下象限中定义一个区域.顺便说一句,本教程提供了在甜甜圈图上进行注释定位的解决方案,可以通过提供xanchor = 'center'yanchor = 'middle'参数来完成.我发现了另一本教程,它提供了一个非常好的示例.在这里,我用您的示例展示它:

I recently struggled with the same problem, and found nothing about whether we can use plotly.tools.make_subplots with plotly.graph_objs.Pie. As I understand this is not possible because these plots have no x and y axes. In the 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 which gives a very nice example. Here I show it with your example:

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坐标是从底部开始的,所以我垂直镜像了您的布局.另外,您可能想在相邻子图之间添加空间(只需在布局中提供较小/更大的数字,例如,右侧为0.31而不是0.33,左侧为0.35而不是0.33).

p.s. Sorry, I realized afterwards that y coordinates start from the bottom, so I mirrored your layout vertically. Also you may want to add space between adjacent subplots (just give slightly smaller/greater numbers in layout, e.g. 0.31 instead 0.33 at right, and 0.35 instead of 0.33 at left corners).

最后,在出于任何目的使用饼图之前,请考虑一下它们是否真的是最佳选择,并考虑使用.

And finally, before using pie charts for any purpose, please think about if they are really the best option, and consider critics like this and this.