【问题标题】:How can I rotate the auto-generated x-axis labels of a matplotlib plot? [duplicate]如何旋转 matplotlib 图的自动生成的 x 轴标签? [复制]
【发布时间】:2016-02-23 10:16:52
【问题描述】:

我想旋转自动生成的绘图的 x 轴标签,以防止标签重叠:

有人建议我matplotlib.pyplot.setp 值得一看,但我不知道如何将它与自动生成的标签结合使用。

如何旋转这些标签?

x = [element[0] for element in eventDuplicates]
y = [element[1] for element in eventDuplicates]
figure = matplotlib.pyplot.figure()
figure.suptitle("event duplicates", fontsize = 20)
matplotlib.pyplot.scatter(x, y, s = 1, alpha = 1)
axes = matplotlib.pyplot.gca()
axes.yaxis.set_major_formatter(FormatStrFormatter("%.0f"))
axes.xaxis.set_major_formatter(FormatStrFormatter("%.0f"))
axes.set_xlim(left = 0)
#axes.set_ylim(bottom = 0)
#matplotlib.pyplot.setp(rotation = 90) # <--- insert magic here
matplotlib.pyplot.xlabel("run numbers")
matplotlib.pyplot.ylabel("event numbers")
matplotlib.pyplot.savefig("diagnostic_event_duplicates_scatter.png")

【问题讨论】:

    标签: python matplotlib axis overlap labels


    【解决方案1】:

    这样的?

    import numpy as np
    import matplotlib.pylab as pl
    
    import matplotlib as mpl
    mpl.rcParams['axes.formatter.useoffset'] = False
    
    x = np.arange(10000000, 10000010, 1)
    y = np.cos(x)
    
    pl.figure()
    pl.subplot(121)
    pl.plot(x,y)
    
    pl.subplot(122)
    pl.plot(x,y)
    locs, labels = pl.xticks()
    pl.setp(labels, rotation=90)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-30
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 2017-07-01
      • 2022-01-20
      相关资源
      最近更新 更多