python - 为什么子图不显示在子图中?
问题描述
我想创建一个只有 3 个子图的 2x2 图。1 个条形图和两个饼图。见代码:
import plotly.graph_objs as go
from plotly.subplots import make_subplots
years = [1980, 1980, 1982, 1983, 1984, 1985, 1986, 1987, 1988,
1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
1998, 1999 ,2000 ,2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
2016, 2017, 2018, 2019, 2020]
freq = [173,1368,2238,4135,5455,6280,7470,6580,7537,8781,10894,14788,20562,27637,
32446,32665,30374,28234,24235,22312,16817,20222,24080,30398,30230,27462,
33582,28908,31648,26579,29121,31216,34574,34271,32570,32531,43390,46761,
55920,34675,0]
values_in = [872641, 13994, 39055, 8985]
labels_in = ['Company', 'Goverment (NGO)', 'Individual', 'University']
values_cty = [297286, 175039, 170002, 66060, 35678, 31959, 26268, 24724, 22058, 17902],
labels_cty = ['Japan', 'Germany', 'USA', 'France' ,'Italy' ,'Switzerland' , 'Korea',
'Great Brtian', 'Netherlands', 'China(PRC)']
fig = make_subplots(
rows=2, cols=2,
specs=[[{"colspan": 2}, None],[{'type':'pie'}, {'type':'pie'}]],
subplot_titles=("Patents","Countries", "Institutions"))
fig.add_trace(go.Bar(x=years, y=freq),row=1,col=1)
fig.add_trace(go.Pie(labels=labels_cty, values=values_cty),
row=2, col=1)
fig.add_trace(go.Pie(labels=labels_in, values=values_in),
row=2, col=2)
我不明白为什么不显示饼图。没有错误代码。我错过了什么吗?
解决方案
在我看来,您只缺少一些子图所需的域属性,例如go.Pie()
:
domain=dict(x=[0, 0.5]
阴谋:
代码:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
years = [1980, 1980, 1982, 1983, 1984, 1985, 1986, 1987, 1988,
1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
1998, 1999 ,2000 ,2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
2016, 2017, 2018, 2019, 2020]
freq = [173,1368,2238,4135,5455,6280,7470,6580,7537,8781,10894,14788,20562,27637,
32446,32665,30374,28234,24235,22312,16817,20222,24080,30398,30230,27462,
33582,28908,31648,26579,29121,31216,34574,34271,32570,32531,43390,46761,
55920,34675,0]
values_in = [872641, 13994, 39055, 8985]
labels_in = ['Company', 'Goverment (NGO)', 'Individual', 'University']
values_cty = [297286, 175039, 170002, 66060, 35678, 31959, 26268, 24724, 22058, 17902]
labels_cty = ['Japan', 'Germany', 'USA', 'France' ,'Italy' ,'Switzerland' , 'Korea',
'Great Brtian', 'Netherlands', 'China(PRC)']
fig = make_subplots(
rows=2, cols=2,
specs=[[{"colspan": 2}, None],[{'type':'pie'}, {'type':'pie'}]],
subplot_titles=("Patents","Countries", "Institutions"))
fig.add_trace(go.Bar(x=years, y=freq),row=1,col=1)
fig.add_trace(go.Pie(
values= [872641, 13994, 39055, 8985],
labels=labels_in,
domain=dict(x=[0, 0.5]),
),
row=2, col=1)
fig.add_trace(go.Pie(
values=values_cty,
labels=labels_cty,
domain=dict(x=[0.5, 1.0]),
),
row=2, col=2)
fig.show()
推荐阅读
- python - 在 Celery Worker 中访问 Celery Group 任务结果
- java - 在文件资源管理器中单击其相关文件时如何打开应用程序?
- google-cloud-platform - GCE Windows 启动脚本
- android - 通过单击android studio中的按钮添加新项目时,自定义列表视图在它们之间显示大空间
- azure - Azure 存储 SAS 密钥在到期日期之前变得陈旧
- python - 如何在 Clojure 的 while 循环中应用嵌套的 for 循环?
- python-3.x - Python 请求并行发出但按顺序接收响应
- postgresql - 错误:“TABLE”第 5 行或附近的语法错误:SELECT * FROM TABLE(str_common_utils.splitstr(p_...)
- javascript - ReactJS DataCloneError:无法在“历史”上执行“pushState”:无法克隆 MouseEvent 对象
- google-oauth - 使用 Cloud Identity 签署文档