I am performing a data analysis from a Dataframe and I need to make a climogram, which is a graph in which the months of the year are located on the horizontal axis, while on the vertical axis they are represented on the left axis the average monthly temperatures by means of points joined by a line, while on the right axis, and by means of bars, the average monthly rainfall is represented. How can I do to generate the two 'labels' of temperature and precipitation on the y axis?
Below is an example of what a climogram looks like and the data of the Dataframe that I am using:
precipitación_y_temperatura_promedio_Medellín_5=pd.merge(precipitación_y_temperatura_promedio_Medellín_3.reset_index(), precipitación_y_temperatura_promedio_Medellín_4.reset_index())
precipitación_y_temperatura_promedio_Medellín_5.set_index('Mes',inplace=True)
print(precipitación_y_temperatura_promedio_Medellín_5)
Precipitación Promedio (mm) Temperatura Promedio (°C)
Mes
Enero 63.2 22.4
Febrero 81.4 22.7
Marzo 129.1 22.7
Abril 170.7 22.4
Mayo 213.5 22.6
Junio 149.4 22.9
Julio 133.1 23.1
Agosto 139.7 23.1
Septiembre 181.8 22.4
Octubre 226.7 21.8
Noviembre 158.9 21.8
Diciembre 104.8 21.9
The data to reproduce the example:
You can't use the same axis because by definition they would share marks, scale and axis labels Y, which we don't want. The key is to use matplotlib.axes.Axes.twinx to generate another instance of
Axes
but sharing the axis Xof the old one.Then each is used for one of the graphs. Since the axis generated by
twinx
overlaps the previous one, it must be used for the temperatures, changing the labels and marks of both Axes to the opposite side each one. In this way we avoid resorting to modifying the transparency that always looks bad.