如何删除Matplotlib轴的一部分

Sam Sirmaxford:

我要绘制一个如图所示的图: 所需的误差线图

这是我的Python3代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0.1,1.1,step=0.1)
y = np.array([0.03,0.03,0.05,0.05,0.06,0.17,0.44,1.37,4.43,6.89])
err = np.array([0.02,0.03,0.05,0.06,0.07,0.23,3.61,4.70,1.2,0.7])

fig, ax = plt.subplots(figsize=(10,8))

ax.errorbar(x,y,yerr=err)

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_position(('axes',-0.02))
ax.spines['left'].set_position(('axes',-0.02))

ax.set_xticks(np.arange(0.0,1.21,0.2))
ax.set_yticks(np.arange(0,11,2))

ax.set_xlabel('X')
ax.set_ylabel('Y')

plt.show()

从上面的代码,我得到这个情节: 带有不需要的轴截面的误差线图

如果我使用ax.set_ylim([0,10])而不是ax.set_yticks(np.arange(0,11,2)),则会得到以下图,该图已将错误栏截断:截断错误栏

任何线索将不胜感激。

我的工作 :

使用ax.spines['left'].set_bounds(0, 10)

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0.1,1.1,step=0.1)
y = np.array([0.03,0.03,0.05,0.05,0.06,0.17,0.44,1.37,4.43,6.89])
err = np.array([0.02,0.03,0.05,0.06,0.07,0.23,3.61,4.70,1.2,0.7])
fig, ax = plt.subplots(figsize=(10,8))

ax.errorbar(x,y,yerr=err)

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_position(('axes',-0.02))
ax.spines['left'].set_bounds(0, 10)

ax.set_xticks(np.arange(0.0,1.21,0.2))
ax.set_yticks(np.arange(0,11,2))

ax.set_xlabel('X')
ax.set_ylabel('Y')

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章