How to plot pie charts as subplots with custom size with Plotly in Python

后端 未结 1 1056
执笔经年
执笔经年 2020-12-31 07:01

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

相关标签:
1条回答
  • 2020-12-31 07:32

    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. 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.

    0 讨论(0)
提交回复
热议问题