混合水平和垂直刻度

Mixing horizontal and vertical ticks

我有下面显示的示例图。我怎样才能让第一个和第三个 x-axis 水平打勾,第二个和第四个打勾垂直?

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y)
plt.xticks(x, labels, rotation='vertical')
plt.margins(0.2)
plt.subplots_adjust(bottom=0.15)
plt.show()

不确定是否有自动执行此操作的方法,但您可以为每个订单号“手动”执行此操作:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y)
plt.xticks(x, labels, rotation='vertical')
plt.margins(0.2)
plt.subplots_adjust(bottom=0.15)
plt.show()

# getting the labels from the axis
ticks_labels = plt.gca().get_xticklabels()

# rotating first and third ticks
ticks_labels[0].set_rotation('horizontal')
ticks_labels[2].set_rotation('horizontal')

ticks_labels 列表中的每个项目都是一个 Text artist,因此您几乎可以更改所有内容(大小、颜色、位置...)