python - 如何更改子图大小并减少子图之间的空间?
问题描述
我用情节创建了一个子图,需要一些关于格式化的建议。
如何调整饼图,使标签和标题之间没有重叠?当然我可以columns_width
,rows_height
但重叠仍然存在。此外,是否有类似于 matplotlibs hspace/wspace 的功能?
代码:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
years = [1980, 1981, 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", "Institutions","Countries"))
fig.add_trace(go.Bar(x=years, y=freq),row=1,col=1)
fig.add_trace(go.Pie(
values= values_in,
labels=labels_in,
domain=dict(x=[0, 0]),
),
row=2, col=1)
fig.add_trace(go.Pie(
values=values_cty,
labels=labels_cty,
domain=dict(x=[0.5,1]),
),
row=2, col=2)
fig.update_layout(showlegend=False)
fig.show()
解决方案
对于您问题的第一部分,请参阅此
要点:您可以通过使用以下值来调整子图的标题位置y
:
fig.layout.annotations[1].update(y=0.5)
关于问题的第二部分,fig.make_subplots()
您可以指定horizontal_spacing
and vertical_spacing
。它们都作为总地块大小的一部分给出。
在您的情况下,您可能必须增加垂直间距以允许子图标题适合。
推荐阅读
- rust - 使用需要所有权的方法时发生移动
- python-3.x - _LightGBMClassifier().fit() 的预期输入是什么?
- java - 升级到 Vert.x 4.2.0 时无法导入“HttpStatusException”
- spring - Spring AOP 切入点表达式匹配调用者的上下文
- poisson - 如何使用泊松分布?到达间隔时间
- android - (Robolectric 单元测试)当运行多个单元测试时,一些测试失败,但运行单个类,它通过
- usb - CP2615 USB 桥接器缺少端点和空接口
- ajax - Ajax POST 请求不起作用 - 在 laravel 刀片中的依赖下拉菜单中
- html - 如何设置儿童 div 的样式
- python - 为 DataClass 创建一个锁定函数