1

I have plots of climate time series for daily mean temperature, precipitation and global radiation. I generated plots like this: https://i.ibb.co/w4x2FMN/temp-mean-1999-2018.png

On x-axis I just generated list of the numbers 1 - 365 which represent the day of year (DOY).

What I actually want is, that the x-axis is devided in month names (as strings) like this: https://i.ibb.co/cL2zc87/rplot.jpg

I tried already a lot of different things but nothing worked.

fig = plt.figure(figsize=(10,10))
ax = plt.axes()

x = np.arange(1,366) # here I define the List with DOY
ax.fill_between(x, temp_cum['min'], temp_cum['max'], color='lightgray', label='1999-2017')
#ax.plot(x, merge_table_99_17_without, color='grey', linewidth=0.3)
ax.plot(x, temp_cum['2018'], color='black', label='2018');
ax.legend(loc='upper left')

ax.set_ylabel('daily mean temperature [°C]')
#ax.set_xlabel('DOY')

plt.show()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

0

First you should convert your numbers to date objects as described in this post. You can use the following function.

import datetime

def serial_date_to_string(srl_no):
    new_date = datetime.datetime(2018,1,1,0,0) + datetime.timedelta(srl_no - 1)
    return new_date.strftime("%Y-%m-%d")

Then you have to format your x-axis to only show the month and not the full dates. This post describes how to do this in detail.

AUBSieGUL
  • 474
  • 1
  • 5
  • 17
0

Thank you very much @AUBSieGUL.

Your second link finally helped me:

import numpy as np
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates


fig = plt.figure(figsize=(12,12))
ax = plt.axes()

### I added this!
# Set the locator
locator = mdates.MonthLocator()  # every month
# Specify the format - %b gives us Jan, Feb...
fmt = mdates.DateFormatter('%b')

numdays = 365
base = datetime.datetime(2018, 1, 1, 0, 0, 0, 0)
date_list = [base + datetime.timedelta(days=x) for x in range(0,numdays)]
###

###replaced all x with date_list
ax.fill_between(date_list, prec_cum['min'], prec_cum['max'], color='lightgray', label='1999-2017')
ax.plot(date_list, merge_table_99_17_cumsum_without, color='grey', linewidth=0.3)
ax.plot(date_list, prec_cum['2018'], color='black', label='2018');
ax.legend(loc='upper left')

ax.set_ylabel('cum. sums of global radiation [kW/m²]')
#ax.set_xlabel('DOY')

### I added this!
X = plt.gca().xaxis
X.set_major_locator(locator)
# Specify formatter
X.set_major_formatter(fmt)
###

plt.show()