Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

python - How to change subplot size and decrease space between subplot?

I created a subplot with plotly and need some advise on formating.

How to adjust the piechart so that there is no overlap between labels and title? Sure i can just columns_width and rows_height but the overlap remains. Moreover, is there function similiar to matplotlibs hspace/wspace?

Code:

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()

enter image description here

question from:https://stackoverflow.com/questions/66064756/how-to-change-subplot-size-and-decrease-space-between-subplot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

for the first part of your question, see this

The gist: you can adjust the subplot's title position by playing with the value of y:

fig.layout.annotations[1].update(y=0.5)

Regarding the second part of your question, in fig.make_subplots() you can specify horizontal_spacing and vertical_spacing. They are both given as a fraction of total plot size.

In your case, you might have to increase the vertical spacing to allow the subplot title to fit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...