How do I change the range of the x-axis with datetimes in matplotlib?

后端 未结 2 933
孤城傲影
孤城傲影 2020-11-28 08:44

I\'m trying to plot a graph of dates on the x-axis and values on the y-axis. It works fine, except that I can\'t get the range of the x-axis to be appropriate. The x-axis ra

2条回答
  •  遥遥无期
    2020-11-28 08:59

    Edit:

    Having seen actual data from the OP, all of the values are at the same date/time. So matplotlib is automatically zooming the x-axis out. You can still manually set the x-axis limits with datetime objects


    If I do something like this on matplotlib v1.3.1:

    import datetime
    import matplotlib.pyplot as plt
    
    x = [datetime.date(2014, 1, 29)] * 3 
    y = [2, 4, 1]
    
    fig, ax = plt.subplots()
    ax.plot_date(x, y, markerfacecolor='CornflowerBlue', markeredgecolor='white')
    fig.autofmt_xdate()
    ax.set_xlim([datetime.date(2014, 1, 26), datetime.date(2014, 2, 1)])
    ax.set_ylim([0, 5])
    

    I get:

    enter image description here

    And the axes limits match the dates that I specified.

提交回复
热议问题