简体   繁体   中英

matplotlib/pyplot - make axes (ordinate and abscissa) bold

Is there a way to make the x=0 and y=0 (the ordinate and abscissa) axes in a Cartesian plot bold?

For example, a simple plot is:

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

Which looks like:

在此处输入图片说明

I can add this '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')

Then I get:

在此处输入图片说明

Is there a way to make this a bit less hacky - an API or a more sophisticated solution?

Please see the code below:

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

在此处输入图片说明 From a practical view, I always use artist to make matplotlib plots, making adjustment of properties much easier.

不错的答案,我想补充一下,除了调整lw设置线宽以供选择外,您还可以在axhlineaxvline关键字中传递color (例如color='k' )以保持黑色

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM