突破Python中的图形边界 [英] Pushing boundaries of graphics in Python

查看:138
本文介绍了突破Python中的图形边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图代表图片2所示的信息,但结果是图片1.如何像图片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()

`

解决方案

基本上,这是此答案的反面:

在matplotlib 2.x中,在边缘设置了自动边距,以确保数据很好地适合于轴尖.默认情况下,它以轴跨度为单位设置为0.05.默认情况下,在2.x之前的matplotlib版本中不存在此边距.但是,在以前的版本中也提供了设置边距的命令:

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

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

取决于上下文.同样可以为两个轴方向设置一个值:

ax.margins(0.05)

另请参见文档.

如果要在整个脚本中设置边距,可以使用

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

在脚本开头(当然与y相同).如果要永久性地完全设置边距,则可能需要更改 matplotlib rc文件.

或者要更改边距,请使用plt.xlim(..)ax.set_xlim(..)手动设置轴的限制,以确保没有空白.

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

`

解决方案

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.

这篇关于突破Python中的图形边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆