matplotlib / pyplot-使轴(纵坐标和横坐标)加粗

时间:2018-11-25 12:24:25

标签: python pandas numpy matplotlib

有没有办法使笛卡尔图中的x = 0和y = 0(纵坐标和横坐标)轴变为粗体?

例如,一个简单的情节是:

x = np.arange(0.01, 5, 0.1)
y = np.log(x)
plt.plot(x,y)
plt.grid()

外观如下:

enter image description here

我可以添加此“ hack”:

x = np.arange(0.01, 5, 0.1)
y = np.log(x)
plt.plot(x,y)
plt.grid()

plt.plot(x,0*x, 'k')
plt.plot(x*0,x, 'k')
plt.plot(x*0,-x, 'k')

然后我得到:

enter image description here

是否有一种方法可以使它变少一点-API还是更复杂的解决方案?

2 个答案:

答案 0 :(得分:2)

请参见下面的代码:

fig, ax = plt.subplots(1, 1, figsize=(9, 9))
ax.plot(x, y)
ax.grid(True)
ax.axhline(y=0, lw=5, color='k')
ax.axvline(x=0, lw=5, color='k')

enter image description here 从实际的角度来看,我总是使用artist绘制matplotlib图,从而使属性调整更加容易。

答案 1 :(得分:1)

很好的回答,我想补充一下,如果您愿意的话,也可以在colorcolor='k'关键字中传递axhline(例如axvline)以使其保持黑色除了调整lw设置线宽以供选择外,