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

时间:2016-09-22 03:24:14

标签: python jupyter-notebook plotly

我一直在尝试使用Jupyter笔记本中的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)

按预期工作: Custom size scattered subplots

但改变饼图的痕迹如下:

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

只能用分散的情节来做这件事吗?我没有在剧情文档中找到任何内容。

1 个答案:

答案 0 :(得分:11)

我最近遇到了同样的问题,但没有发现我们是否可以将plotly.tools.make_subplotsplotly.graph_objs.Pie一起使用。据我所知,这是不可能的,因为这些图没有x轴和y轴。在Pie domain中,他们通过提供{'x': [0.0, 0.5], 'y': [0.0, 0.5]} dict进行子图,例如xanchor = 'center'定义了总绘图空间左下象限中的一个区域。顺便说一下,本教程在环形图表中提供了注释定位的解决方案,提供yanchor = 'middle'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) 参数可以做些什么。我发现original tutorial给出了一个非常好的例子。在这里,我用你的例子展示它:

$dec = is_float($spaces);
if ($dec === true){
   echo "Error"
 } 

one other tutorial

P.S。对不起,我后来意识到y坐标从底部开始,所以我垂直镜像了你的布局。此外,您可能希望在相邻的子图之间添加空格(只需在布局中给出略小/更大的数字,例如0.31而不是0.33在右边,0.35而不是0.33在左角)。

最后,在将饼图用于任何目的之前,请考虑它们是否真的是最佳选择,并考虑批评者如Plotly pie charts subplots examplethis