python - Plotly:如何创建奇数个子图?
问题描述
我希望第 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 个图,则会引发错误。
解决方案
您可以通过正确设置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()
推荐阅读
- javascript - 如何使用 Puppeteer 检查特定脚本元素是异步还是延迟
- html - 在Angular 8中提交成功后如何清除验证错误?
- flutter - 在屏幕底部移动 Stack 的子项
- javascript - div内滚动amchart图
- c# - 与 REST 客户端应用程序共享实体框架生成的模型的方法
- mysql - 将多个 MySQL 数据库推送到 ElasticSearch
- angular - Angular通过构造函数调用组件失败但可以在打字稿文件中正常导入
- automation - Isabelle 的自动求解器找不到正确的定理
- mongodb - Mongo $in 查询适用于独立的 mongo 实例,但不适用于 AWS DocumentDB 实例
- javascript - 我收到 java.security.cert.CertificateException:找不到名称匹配。Spring Boot 肥皂客户端