【问题标题】:How to subplot pie chart in plotly?如何在plotly中绘制饼图?
【发布时间】:2019-07-25 07:49:02
【问题描述】:

如何在“fig”中绘制“pie1”,使其位于“第一个”位置。这就是我的做法,但它不起作用

    import pandas as pd
    import numpy as np
    import seaborn as sns
    import plotly.offline as pyp
    import plotly.graph_objs as go
    from plotly import tools
    import plotly.plotly as py
    from plotly.offline import iplot,init_notebook_mode
    from IPython.core.display import HTML
    import plotly.io

    df1=pd.read_excel('file.xlsx',sheet_name='sheet1',index=False)
    con_pivot=pd.pivot_table(con,index='Category',values=('Payment'),aggfunc='sum',margins=True,margins_name='Total')

    fig = tools.make_subplots(rows=2, cols=2, subplot_titles=('The first','3','2','4'))

    pie1=go.Pie(labels=con_pivot.index,values=con_pivot.values)
    fig.append_trace(pie1,1,1)
    pyo.plot(fig)

任何帮助将不胜感激。 谢谢

【问题讨论】:

    标签: python pandas ipython plotly subplot


    【解决方案1】:

    使用 plotly 中的make_subplots 函数实现并排饼图的方法如下(非常感谢@Oysiyl 的输入数据):

    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    from plotly.offline import plot
    
    
    fig = make_subplots(rows=1, cols=2, specs=[[{"type": "pie"}, {"type": "pie"}]])
    
    
    fig.add_trace(go.Pie(
         values=[16, 15, 12, 6, 5, 4, 42],
         labels=["US", "China", "European Union", "Russian Federation",
                 "Brazil", "India", "Rest of World"
                 ],
         domain=dict(x=[0, 0.5]),
         name="GHG Emissions"), 
         row=1, col=1)
    
    fig.add_trace(go.Pie(
         values=[27, 11, 25, 8, 1, 3, 25],
         labels=["US", "China", "European Union", "Russian Federation",
                 "Brazil", "India", "Rest of World"
                 ],
         domain=dict(x=[0.5, 1.0]),
         name="CO2 Emissions"),
        row=1, col=2)
    
    plot(fig)
    

    【讨论】:

      【解决方案2】:

      您应该查看domain 参数以从饼图制作子图。例如,要在 1 行(x 轴)中制作两个饼图,您可以指定第一个和第二个图将占据多少空间(第一个图从 0% 到 50%,第二个从 50% 到 100%)。

      代码:

      from plotly import tools
      import plotly.offline as py
      import plotly.graph_objs as go
      
      trace1 = go.Pie(
           values=[16, 15, 12, 6, 5, 4, 42],
           labels=["US", "China", "European Union", "Russian Federation",
                   "Brazil", "India", "Rest of World"
                   ],
           domain=dict(x=[0, 0.5]),
           name="GHG Emissions",
           hoverinfo="label+percent+name",
      )
      trace2 = go.Pie(
           values=[27, 11, 25, 8, 1, 3, 25],
           labels=["US", "China", "European Union", "Russian Federation",
                   "Brazil", "India", "Rest of World"
                   ],
           domain=dict(x=[0.5, 1.0]),
           name="CO2 Emissions",
           hoverinfo="label+percent+name",
      )
      layout = go.Layout(title="Global Emissions 1990-2011",)
      data = [trace1, trace2]
      fig = go.Figure(data=data, layout=layout)
      py.plot(fig, filename='simple-pie-subplot')
      

      输出: 如果需要,您还可以查看documentation 并在此处找到 2x2 子图的示例。

      【讨论】:

      • @verbamore,我的回答对你有帮助吗?
      • 感谢您的回答,但它没有提供预期的结果。馅饼太小(不知道如何控制大小)。我的方式(上图)适用于 Bars,但不适用于 Pies。这很奇怪,我不知道如何解释。
      • @Verbamore,如果您使用子图创建两个条形图,您还会得到小于一个图的图。它是如何工作的。提供您预期结果的图片,也许我可以帮助您实现您想要的结果
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      相关资源
      最近更新 更多