0

How is x-ticks manually set in seaborn sns in python?

This might be a duplicate of: How to set x axis ticklabels in a seaborn plot, but the solution did not work for us.


enter image description here

We would like the x-ticks to start from 2020-01, but as data is only available from 2020-02, it doesn't automatically start the x-ticks on 2020-01. Although it will be an empty space, we would still like to include 2020-01. Following is the function.

def create_lineplot(dataframe):

    months = mdates.MonthLocator()  # every month
    years_fmt = mdates.DateFormatter('%Y-%m')  # This is a format. Will be clear in Screenshot

    # Filtering data to only select relevant columns and data from the year 2020
    dataframe = dataframe[['dev_id', 'temp_20', 'temp_60', 'datetime']]
    dataframe["datetime"] = pd.to_datetime(dataframe["datetime"])
    soil = dataframe[dataframe['datetime'].dt.year == 2020]

    fig, axes = plt.subplots(figsize=(20, 2))

    mdf = pd.melt(soil, id_vars=['datetime', 'dev_id'], var_name=['Temperature'])

    g = sns.relplot(data=mdf, x='datetime', y='value', kind='line', hue='Temperature', height=5, aspect=3)
    g._legend.remove()

    axes.xaxis.set_major_locator(months)
    axes.xaxis.set_major_formatter(years_fmt)
    axes.xaxis.set_minor_locator(months)

    plt.xticks(rotation='vertical')
    plt.tight_layout()
    plt.legend(loc='upper right')

    plt.savefig('lineplot.png')

    plt.show()

When we include following:

g.set_xticklabels(['2020-01','2020-02','2020-03','2020-04','2020-05','2020-06','2020-07','2020-08', '2020-09', '2020-10', '2020-11', '2020-12'])

between

g = sns.relplot(data=mdf, x='datetime', y='value', kind='line', hue='Temperature', height=5, aspect=3)
--- HERE ---
g._legend.remove()

then the tick is added as desired, but the values are stretched so it seems like there is data in 2020-01 as well.

enter image description here

Following is an example of the data:

enter image description here

Bonus

How to align the ticks after adding a new?

enter image description here

Buster3650
  • 470
  • 2
  • 8
  • 19
  • 1
    Since you want the plot to start at a lower x value than you have data for, you first need to set the x limit with `ax.set_xlim()`. See this question: https://stackoverflow.com/questions/25212986/how-to-set-some-xlim-and-ylim-in-seaborn-lmplot-facetgrid – Arne Dec 06 '21 at 13:24
  • Not sure if understood correctly, so in our example we say something like g.set(xlim=('2020-01', None)), but this doesn't change anything on our graph – Buster3650 Dec 06 '21 at 13:36
  • Sorry, was just placed wrong, but not it works! – Buster3650 Dec 06 '21 at 13:40
  • Although it is at the very start of the x-axis, is there anyway to move the first tick a little farther to the right? – Buster3650 Dec 06 '21 at 13:56
  • 1
    Sure, just set the lower `xlim` value to something a little smaller than your first tick value. – Arne Dec 06 '21 at 14:02
  • That worked. Thank you very much for your help and effort! As your answer is written as a comment I can't tick you off as the answer :) – Buster3650 Dec 06 '21 at 14:13
  • 1
    That's fine, I'm glad I could help. – Arne Dec 06 '21 at 14:32

0 Answers0