【问题标题】:How to change the xticks in matplotlib如何更改 matplotlib 中的 xticks
【发布时间】:2021-09-28 07:01:43
【问题描述】:

我正在尝试重命名 matplotlib 中的 yticks。
通过这些代码行,我得到了第一个数字。

from matplotlib import pyplot as plt
incr = -2
fig = plt.figure(figsize=(4,2))
ax = fig.add_subplot(111)
ax.set_yticks([])

yy = [[0.1, 0.2, 0.4], [0.2,0.3, 0.5], [0.4, 0.3, 0.5]]
for i in range(len(yy)):
    incr += 2
    xx = list(range(len(yy[i])))
    xx = [x+incr for x in xx]
    ax.plot(xx, yy[i])
    ax.plot([incr,incr], [0,.3])

我怎样才能改变这个图中的xticks

【问题讨论】:

    标签: python matplotlib axis-labels


    【解决方案1】:
    from matplotlib import pyplot as plt
    
    incr = -2
    fig = plt.figure(figsize=(4,2))
    ax = fig.add_subplot(111)
    ax.set_yticks([])
    xlabel = []
    yy = [[0.1, 0.2, 0.4], [0.2,0.3, 0.5], [0.4, 0.3, 0.5]]
    for i in range(len(yy)):
        incr += 2
        xx = list(range(len(yy[i])))
        xx = [x+incr for x in xx]
        ax.plot(xx, yy[i])
        ax.plot([incr,incr], [0,.3])
        xlabel.append(incr)
    
    ax.set_xticks(xlabel)
    ax.set_xticklabels(['set 1', 'set 2', 'set 3'], rotation=45)
    
    for label in ax.get_xticklabels():
        label.set_horizontalalignment('left')
    

    输出:

    【讨论】:

      【解决方案2】:

      pylab 和 xticks 也可以做到这一点

      import matplotlib
      import matplotlib.pyplot as plt
      x = [0,1,2]
      y = [90,40,65]
      labels = ['high', 'low', 37337]
      plt.plot(x,y, 'r')
      plt.xticks(x, labels, rotation='vertical')
      plt.show()
      

      https://matplotlib.org/stable/gallery/ticks_and_spines/ticklabels_rotation.html

      https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.xticks.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2021-09-24
        相关资源
        最近更新 更多