簡體   English   中英

如何在plotly中子繪制餅圖?

[英]How to subplot pie chart in plotly?

如何在“無花果”中對“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)

任何幫助將不勝感激。 謝謝

使用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)

在此處輸入圖片說明

您應該查看參數以從餅圖中制作子圖。 例如,要在 1 行 (xaxis) 中制作兩個餅圖,您可以指定第一幅圖和第二幅圖將占據多少位置(第一幅圖從 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')

輸出: 簡單的餅圖子圖 如果需要,您還可以查看文檔並在此處找到 2x2 子圖的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM