在Python中推动图形界限

2024-09-23 04:29:15 发布

您现在位置:Python中文网/ 问答频道 /正文

picture 1

picture 2

我试图表示如图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()

`


Tags: 信息indexplotfigrangepltcountryregion
1条回答
网友
1楼 · 发布于 2024-09-23 04:29:15

这里基本上是this answer的倒数:

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

另见the documentation。你知道吗

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

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

在脚本的开头(当然y也是)。如果您想完全且永远地设置边距,您可能需要更改matplotlib rc file中的相应行。你知道吗

除了更改边距之外,还可以使用plt.xlim(..)ax.set_xlim(..)手动设置轴的限制,这样就不会留下空白。你知道吗

相关问题 更多 >