landviser
landviser

Reputation: 73

How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot?

I am trying to plot several columns from pandas dataframe on the same graph, temperature as linear and rain as bars sharing the same x-Date and separate y-scales.

I figured out how to plot them as separate LINE (x-y scatter) and they share the same x-date. I can play with colors too. Eventually, I want 3 lines for temperatures and columns(bars) for rain and make second y-scale (for rain) bigger so the bars not overlapping with Temperatures. Is it possible with pyplot?

import matplotlib.dates as mdates
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()

myDates = doystats['Fdate']
myValues = doystats['T2M_mean']

ax1.plot(myDates, myValues, 'g')
ax1.set_xlabel('Dates')

# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('Temperature, C')
ax1.tick_params('y', colors='g')

ax2 = ax1.twinx()
myRain = doystats['PRECTOT_mean']

ax2.plot(myDates, myRain, color='b')
ax2.set_ylabel('Precipitation, mm', color='b')
ax2.tick_params('y', colors='b')

myFmt = mdates.DateFormatter('%b-%d')
ax1.xaxis.set_major_formatter(myFmt)

fig.tight_layout()
plt.show()

this code produces two line graphs with two y-axes.

enter image description here

If I change it to plot both vs 'DOY_'(integer) instead of "Fdate' (datetime) - without DateFormatter piece (that results in error now, if included), I can produce line/bar graph as below.

myDates = doystats['DOY_']
ax2.bar(myDates, myRain, color='b')
#myFmt = mdates.DateFormatter('%b-%d')
#ax1.xaxis.set_major_formatter(myFmt)

enter image description here

The workaround maybe to plot vs "DOY_" but somehow recalculate date from it on the fly and format as MON? Here is the link to the data https://pylab-landviser.notebooks.azure.com/j/edit/PR2_1981-2018_stat.csv and to the whole Jupiter NB https://pylab-landviser.notebooks.azure.com/j/notebooks/WeatherDaily-Copy.ipynb

Upvotes: 2

Views: 6006

Answers (1)

landviser
landviser

Reputation: 73

I ended up using twinx() and twiny() to plot bar graph for rain on integer DOY (day of the year) second x-axis (and hide it in the display) and second y axis (set limit to 30 mm). All temperatures are plotted on first x (formatted datetime) and first y axes (self-adjusting to the temperature range). Below is full code:

matplotlib.rc('figure', figsize=(12, 5))   # this is to overwrite default aspect of graph to make x-axis longer

fig, ax1 = plt.subplots()

# data to be plotted from dataframe - temperatures as line graphs
Dates = doystats['Fdate']
Tmean = doystats['T2M_mean']
Tmax = doystats['T2M_MAX_mean']
Tmin = doystats['T2M_MIN_mean']

ax1.plot(Dates, Tmean, 'g', Dates, Tmax, 'r', Dates, Tmin, 'k')

# Make the y-axis label
ax1.set_ylabel('Temperature (C)')
ax1.tick_params('y')

# Creating twin axes for precipitation as a bar graph on secondary XY axes
ax2 = ax1.twiny()
ax3 = ax2.twinx()

#data for bar graph
doy = doystats['DOY_']
myRain = doystats['PRECTOT_mean']

ax3.bar(doy, myRain, color='b')
ax2.set_xticks([])                # to hide ticks on the second X-axis - top of the graph
ax3.set_ylabel('Precipitation (mm)', color='b')
ax3.tick_params('y', colors='b')
ax3.set_ylim(top=30)

# Formatting Date X-axis with monthly scale
months = mdates.MonthLocator()  # every month
days = mdates.DayLocator()       # every day
myFmt = mdates.DateFormatter('%b')
ax1.xaxis.set_major_locator(months)
ax1.xaxis.set_major_formatter(myFmt)
ax1.xaxis.set_minor_locator(days)

# Formatting second X-axis (DOY integers) to disappear
ax2.xaxis.set_major_formatter(plt.NullFormatter())

# Displaying grid for the Date axis
ax1.grid(True)

fig.tight_layout()
# Saving graph to file
plt.savefig(filename + '_annual.png',dpi=300,transparent=True)

which makes chart below: Bar and line charts on two sets of XY axes

Upvotes: 3

Related Questions