简体   繁体   English

matplotlib中的刻度线

[英]tick marks in matplotlib

I am creating a subplot using matplotlib, where the top x-axis is different from the bottom x-axis. 我正在使用matplotlib创建一个子图,其中顶部x轴与底部x轴不同。 I added the xticks and xlabels to the top myself. 我自己将xticks和xlabels添加到顶部。

I would like there to be xtick marks corresponding to the bottom x-axis at the bottom of the middle and top subplots, where the xticks point outwards (or down) - like at the very bottom. 我希望在中间和顶部子图的底部有与底部x轴相对应的xtick标记,其中xticks指向外部(或向下),就像在底部一样。 Is there a way to do this? 有没有办法做到这一点?

This is the code I am using so far to customize the ticks: 到目前为止,这是我用来自定义刻度的代码:

f, (ax1, ax2, ax3) = plt.subplots(3, sharex=False, sharey=False)
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)

ax3.get_xaxis().set_tick_params(direction='out', top='off', which='both')
ax2.get_xaxis().set_tick_params(direction='out', bottom='on', top='off', which='both')


ax1.minorticks_off()
ax1.get_xaxis().tick_top()
ax1.set_xticks([np.divide(1.0,100.0), np.divide(1.0,50.0), np.divide(1.0,35.0),
                np.divide(1.0,20.0), np.divide(1.0,10.0), np.divide(1.0,5.0),
                np.divide(1.0,3.0), np.divide(1.0,2.0), np.divide(1.0,1.0)])
ax1.set_xticklabels([100, 50, 35, 20, 10, 5, 3, 2, 1])

I am finding that since I made customized xticks for the top plot, I can't do this, and specifying the direction to be 'out' in the bottom subplot ruins the tick marks in the middle. 我发现,由于我为顶部绘图制作了自定义的xticks,因此无法执行此操作,而在底部子绘图中指定“向外”的方向会破坏中间的刻度线。 Since there is no space between the subplots, the top of the bottom plot shares its x-axis with the bottom of the middle subplot, etc... 由于子图之间没有空间,因此底部图的顶部与中间子图的底部共享x轴,依此类推...

Is there a workaround for this? 有没有解决方法?

在此处输入图片说明

You can draw the middle axes above the bottom axes by setting the respective z orders. 您可以通过设置相应的z顺序在底部轴上方绘制中间轴。 The top ticks can be done w/a call to axvline. 可以通过调用axvline来完成最上面的刻度。

import matplotlib.pyplot as plt
import numpy as np

f, (ax1, ax2, ax3) = plt.subplots(3, sharex=False, sharey=False)
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)

ax3.get_xaxis().set_tick_params(direction='out', top='off', which='both')
ax2.get_xaxis().set_tick_params(direction='out', bottom='on', top='off', which='both')


ax1.minorticks_off()
ax1.get_xaxis().tick_top()
ax1.set_xticks([np.divide(1.0,100.0), np.divide(1.0,50.0), np.divide(1.0,35.0),
                np.divide(1.0,20.0), np.divide(1.0,10.0), np.divide(1.0,5.0),
                np.divide(1.0,3.0), np.divide(1.0,2.0), np.divide(1.0,1.0)])
ax1.set_xticklabels([100, 50, 35, 20, 10, 5, 3, 2, 1])

for i, ax in enumerate((ax3, ax2, ax1)):
    ax.set_zorder(i)
for tick in ax2.xaxis.get_ticklocs():
    ax2.axvline(tick, ymin=0.9)
plt.show()    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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