Rotate axis text in python matplotlib

前端 未结 13 896
暗喜
暗喜 2020-11-22 14:50

I can\'t figure out how to rotate the text on the X Axis. Its a time stamp, so as the number of samples increase, they get closer and closer until they overlap. I\'d like

相关标签:
13条回答
  • 2020-11-22 15:27
    import pylab as pl
    pl.xticks(rotation = 90)
    
    0 讨论(0)
  • 2020-11-22 15:31

    If you want to apply rotation on the axes object, the easiest way is using tick_params. For example.

    ax.tick_params(axis='x', labelrotation=90)
    

    Matplotlib documentation reference here.

    This is useful when you have an array of axes as returned by plt.subplots, and it is more convenient than using set_xticks because in that case you need to also set the tick labels, and also more convenient that those that iterate over the ticks (for obvious reasons)

    0 讨论(0)
  • 2020-11-22 15:31

    The simplest solution is to use:

    plt.xticks(rotation=XX)
    

    but also

    # Tweak spacing to prevent clipping of tick-labels
    plt.subplots_adjust(bottom=X.XX)
    

    e.g for dates I used rotation=45 and bottom=0.20 but you can do some test for your data

    0 讨论(0)
  • 2020-11-22 15:33

    Many "correct" answers here but I'll add one more since I think some details are left out of several. The OP asked for 90 degree rotation but I'll change to 45 degrees because when you use an angle that isn't zero or 90, you should change the horizontal alignment as well; otherwise your labels will be off-center and a bit misleading (and I'm guessing many people who come here want to rotate axes to something other than 90).

    Easiest / Least Code

    Option 1

    plt.xticks(rotation=45, ha='right')
    

    As mentioned previously, that may not be desirable if you'd rather take the Object Oriented approach.

    Option 2

    Another fast way (it's intended for date objects but seems to work on any label; doubt this is recommended though):

    fig.autofmt_xdate(rotation=45)
    

    fig you would usually get from:

    • fig = plt.figure()
    • fig, ax = plt.subplots()
    • fig = ax.figure

    Object-Oriented / Dealing directly with ax

    Option 3a

    If you have the list of labels:

    labels = ['One', 'Two', 'Three']
    ax.set_xticklabels(labels, rotation=45, ha='right')
    

    Option 3b

    If you want to get the list of labels from the current plot:

    # Unfortunately you need to draw your figure first to assign the labels,
    # otherwise get_xticklabels() will return empty strings.
    plt.draw()
    ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
    

    Option 4

    Similar to above, but loop through manually instead.

    for label in ax.get_xticklabels():
      label.set_rotation(45)
      label.set_ha('right')
    

    Option 5

    We still use pyplot (as plt) here but it's object-oriented because we're changing the property of a specific ax object.

    plt.setp(ax.get_xticklabels(), rotation=45, ha='right')
    

    Option 6

    This option is simple, but AFAIK you can't set label horizontal align this way so another option might be better if your angle is not 90.

    ax.tick_params(axis='x', labelrotation=45)
    

    Edit: There's discussion of this exact "bug" and a fix is potentially slated for v3.2.0: https://github.com/matplotlib/matplotlib/issues/13774

    0 讨论(0)
  • 2020-11-22 15:36

    My answer is inspired by cjohnson318's answer, but I didn't want to supply a hardcoded list of labels; I wanted to rotate the existing labels:

    for tick in ax.get_xticklabels():
        tick.set_rotation(45)
    
    0 讨论(0)
  • 2020-11-22 15:37

    Try pyplot.setp. I think you could do something like this:

    x = range(len(time))
    plt.xticks(x,  time)
    locs, labels = plt.xticks()
    plt.setp(labels, rotation=90)
    plt.plot(x, delay)
    
    0 讨论(0)
提交回复
热议问题