python 边界,突破Python中的图形边界

613e1eca1ea995965c87ccd135084581.png

NGfVe.png

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)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值