简体   繁体   中英

How to get ticks on the bottom subplot in matplotlib

I am plotting 8 subplots in a figure as follows:

import matplotlib.pyplot as plt
fig, axs = plt.subplots(8,sharex="col" )
label = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
data = [0.6, 0.4, 1.3, 0.8, 0.9, 1.0, 1.6, 0.2]
plt.xlim(0,2)
for i in range(8):
    axs[i].set_xlim([0, 2])
    axs[i].axvline(data[i],linestyle='--')
    axs[i].set_yticks(())
    axs[i].set_ylabel(label[i], rotation=0, ha='right', va='center')
axs[7].tick_params(axis='x', direction='out')
plt.show()

在此处输入图像描述

It all works fine except there are no ticks on the x-axis. What am I doing wrong?

I am answering your question in the comments. You can hide the ticks for all axes except the last one using the following

for i in range(8):
    axs[i].set_xlim([0, 2])
    axs[i].axvline(data[i],linestyle='--')
    axs[i].set_yticks(())
    axs[i].set_ylabel(label[i], rotation=0, ha='right', va='center')
    if i!=7:
        axs[i].tick_params(axis='x', direction='out', length=0) # Hide the ticks for all but last axis
        # axs[i].xaxis.set_tick_params(direction='out', length=0) # This is 2nd way to do

在此处输入图像描述

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