【问题标题】:Plotly: How to create an odd number of subplots?Plotly:如何创建奇数个子图?
【发布时间】:2021-01-12 11:59:28
【问题描述】:

我希望第 5 个子图位于第三行两列的中心。 (我尝试通过添加 domain 参数来做到这一点)。这是重现它的代码-

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

continent_df = pd.read_csv('https://raw.githubusercontent.com/vyaduvanshi/helper-files/master/continent.csv')
temp_cont_df = pd.pivot_table(continent_df, index='continent', aggfunc='last').reset_index()

fig = make_subplots(rows=3, cols=2, specs=[[{'type':'pie'},{'type':'pie'}],[{'type':'pie'},{'type':'pie'}],
                                           [{'type':'pie'},{'type':'pie'}]])

fig.add_pie(labels=continent_df.continent, values=continent_df.new_cases, row=1,col=1)
fig.add_pie(labels=continent_df.continent, values=continent_df.new_deaths, row=1,col=2)
fig.add_pie(labels=continent_df.continent, values=continent_df.new_recovered, row=2,col=1)
fig.add_pie(labels=continent_df.continent, values=continent_df.new_tests, row=2,col=2)
fig.add_pie(labels=temp_cont_df.continent, values=temp_cont_df.active_cases, row=3,col=1,domain={'x':[0.25,0.75],'y':[0,0.33]})

如果我没有在 specs 参数中包含第 6 个图,则会引发错误。

【问题讨论】:

  • doc 对您有帮助吗?
  • 确实如此!只需将 'colspan':2 添加到倒数第二个规范参数并制作最后一个 None 就可以了。

标签: python plotly data-visualization plotly-python


【解决方案1】:

您可以通过正确设置domain 来实现此目的。这是一个示例,四个角各有一个图形,中间有一个图形。

情节

完整代码:

import plotly
import plotly.offline as py
import plotly.graph_objs as go

labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
domains = [
    {'x': [0.0, 0.33], 'y': [0.0, 0.33]},
    {'x': [0.33, 0.66], 'y': [0.33, 0.66]},
    {'x': [0.0, 0.33], 'y': [0.66, 1.0]},
    {'x': [0.66, 1.00], 'y': [0.0, 0.33]},
    {'x': [0.66, 1.0], 'y': [0.66, 1.00]},
]
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)
fig.show()

【讨论】:

  • 谢谢!我能够以domain 方式(您的方式)以及通过编辑我的specs 参数的最后一位来实现它。无论哪种方式都可以正常工作
  • specs=[[{'type':'pie'},{'type':'pie'}],[{'type':'pie'},{'type':'pie'}], [{'type':'pie','colspan':2},None]]
  • domain 看起来更好,因为与specs 方式的严格列和行限制相比,控制更精细(尽管我确信也有解决方法)跨度>
猜你喜欢
  • 2021-07-21
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 2020-09-25
  • 2021-01-02
  • 2017-01-04
  • 2022-01-16
  • 2020-07-23
相关资源
最近更新 更多