Search code examples
pythondatetimematplotlibformattingxticks

Display only data on axis that is present in the figure


example

How do I hide the 32. and Jun, 14 2020 tickers that are not getting used. They don't exist in the data I provided.

I'm setting locators like this:

plt.gca().xaxis.set_major_locator(DayLocator())
plt.gca().yaxis.set_major_locator(MultipleLocator(1))

Solution

  • For the y axis you should use:

    plt.gca().set_ylim([27, 31])
    

    The same for the x axis, but you should use the date format. Can you provide the line of code where you do the plot? As an example, if your x axis data are located in a 'Date' column of a df dataframe, you could use:

    plt.gca().set_xlim([df['Date'].iloc[0], df['Date'].iloc[-1]])