gpt4 book ai didi

python - 如何删除一段 matplotlib 轴

转载 作者:行者123 更新时间:2023-12-03 20:51:50 24 4
gpt4 key购买 nike

我想要一个像图中所示的情节:
Desired errorbar plot

这是我的 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()

从上面的代码,我得到这个情节:
Errorbar plot with an unwanted axis section

如果我使用 ax.set_ylim([0,10])而不是 ax.set_yticks(np.arange(0,11,2)) ,我得到下图截断了误差条:
Truncated error bar

任何线索将不胜感激。

最佳答案

使用 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')
enter image description here

关于python - 如何删除一段 matplotlib 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62472497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com