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
433 views
in Technique[技术] by (71.8m points)

matplotlib - Pushing boundaries of graphics in Python

picture 1

picture 2

I'm trying to represent information as it shown on picture 2, but result is picture 1. How can I push borders as on picture 2. `

plot = newData.copy() 
del plot['Region']; del plot['Country']; del plot['2016 Rank'] 
plot.index = range(0, 20) 
fig = plt.figure(figsize=(8,4))
plt.title('Springfield') 
plt.ylabel('CPI') 

plt.yticks(range(20,90,10)) 
plt.xticks(num.arange(5), ( '2012 Score', '2013 Score', '2014 Score', '2015 Score', '2016 Score', )) 
plt.grid(axis='both', which='major') 

for x in plot.values: 
    plt.plot(x, color='gray', linewidth=1, marker='o', markerfacecolor='gray', markersize=6) 

plt.show()

`

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Here basically the inverse of this answer applies:

In matplotlib 2.x there is an automatic margin set at the edges, which ensures the data to be nicely fitting within the axis spines. By default it is set to 0.05 in units of axis span. This margin is not present by default in matplotlib versions prior to 2.x. However the command to set the margins is present also in previous versions:

plt.margins(x=0.04, y=0.06)

or

ax.margins(x=0.04, y=0.06)

depending on the context. Setting a single value for both axis directions is equally possible:

ax.margins(0.05)

Also see the documentation.

In case you want to set the margin in the whole script, you can use

plt.rcParams['axes.xmargin'] = 0.05
plt.rcParams['axes.ymargin'] = 0.05

at the beginning of your script (same for y of course). If you want set the margin entirely and forever, you might want to change the according line in the matplotlib rc file.

Alternatively to changing the margins, use plt.xlim(..) or ax.set_xlim(..) to manually set the limits of the axes such that there is no white space left.


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

1.4m articles

1.4m replys

5 comments

57.0k users

...