Home > Back-end >  Changing date format and x-axis tick labels
Changing date format and x-axis tick labels

Time:10-28

I have read in a monthly temperature anomalies csv file using Pandas read.csv() function. Years are from 1881 to 2022. I excluded the last 3 months of 202 to avoid -999 values). Date format is yyyy-mm-dd. How can I just plot the year and only one value instead of 12 on the x-axis (i.e., I don't need 12 1851s, 1852s, etc.)?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
import matplotlib.dates as mdates

ds = pd.read_csv('path_to_file.csv', header='infer', engine='python', skipfooter=3)
dates = ds['Date']
tAnoms = ds[' Berkeley Earth 2m Air Temperature (degree C) 0N-90N;0E-360E']

fig = plt.figure(figsize=(10,10))
ax = plt.subplot(111)

ax.plot(dates,tAnoms)
ax.plot(dates,tAnoms.rolling(60, center=True).mean())
ax.xaxis.set_major_locator(mdates.YearLocator(month=1) # EDIT
years_fmt = mdates.DateFormatter('%Y') # EDIT 2
ax.xaxis.set_major_formatter(years_fmt) # EDIT 2

plt.show()

EDIT: adding the following gives me the 2nd plot EDIT 2: Gives me yearly values, but only from 1970-1975. 3rd plot

CSV file Plot How I want the dates to look like (minus month and day) Plot after edits Plot after using set_major_formatter

CodePudding user response:

You could:

  1. Create a new column year from your Date column.
  2. Compute the average temperature for each year (using mean or median): df.groupby(['year']).mean()

CodePudding user response:

So, I found a good, but maybe not perfect solution. First thing I needed to do was use parse_dates & infer_datetime_format when reading in the csv file. Then, convert dates to pydatetime(). mdates.AutoDateLocator() was what I needed along with set_major_formatter. Not sure how I could manually change the interval, however (e.g., change to every 10 years or 25 years instead of using the default. This does work well enough though.

ds = pd.read_csv('path_to_file.csv', parse_dates=['Date'], infer_datetime_format=True,
                 header='infer', engine='python', skipfooter=3)

dates = ds['Date'].dt.to_pydatetime() # Convert to pydatetime()
tAnoms = ds[' Berkeley Earth 2m Air Temperature (degree C) 0N-90N;0E-360E']

fig = plt.figure(figsize=(10,10))
ax = plt.subplot(111)

# Produce plot
ax.plot(dates,tAnoms.rolling(60, center=True).mean())

# Use AutoDateLocator() from matplotlib.dates (mdates)
# Set date format to years
ax.xaxis.set_major_locator(mdates.AutoDateLocator())
years_fmt = mdates.DateFormatter('%Y')
ax.xaxis.set_major_formatter(years_fmt)

plt.show()

enter image description here

  • Related