【问题标题】:Mixing horizontal and vertical ticks混合水平和垂直刻度
【发布时间】:2021-08-16 05:59:29
【问题描述】:

我有下面显示的示例图。如何使第一个和第三个 x 轴刻度为水平,第二个和第四个刻度为垂直?

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

【问题讨论】:

    标签: python matplotlib xticks


    【解决方案1】:

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

    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,因此您几乎可以更改所有内容(大小、颜色、位置……)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      相关资源
      最近更新 更多