2

I wanted to reproduce a Walter-Leigh climate diagram and I have some trouble with the precipitation axis. It needs to be linear for range 0-100 and then logarithmic for 100+ (usually it is cut off at 300). Temperature also must be on the left and precipitation on the right axis.

I have tried to use ggplot package, but without any success - it just does not work so well for python as it does in R. I also tried to use cloupy package which has this diagram as a built-in function, but also it did not work well and it requires data in a specific format which I do not have. In conclusion, I wanted to make my own function which has specific features I need for my purpouses.

Now I have tried to copy a solution for a similar problem I saw in this answer (https://stackoverflow.com/a/34797574/14299433), but it seems like it just pastes the logarithmic part over the linear part. The reasoning for this can bee seen if you change the "size" feature in divider.append_axes function.

Here are the results and my code:

import pandas as pd
import numpy as np 
import matplotlib.pyplot as pl
from mpl_toolkits.axes_grid1 import make_axes_locatable

#the data
t=[8.2,8.9,11.2,15.0,19.8,24.5,27.1,26.7,21.6,17.5,13.3,9.7]
p=[77.2,66.2,58.5,63.8,60.4,44.6,24.3,36.7,83.0,73.3,123.0,109.5]

#the code, much of it I do not understand fully, it was copied from the solution linked and modified for my case
fig, axt=pl.subplots()

axt.set_xlabel("Months")
axt.set_ylabel("Temperature")
pl.ylim([0,50])
axt.plot(m,t, "r")

axp=axt.twinx()

axp.plot(m,p)
axp.set_yscale("linear")
axp.set_ylim([0,100])
axp.spines['top'].set_visible(False)
axp.set_ylabel("Precipitation")

divider = make_axes_locatable(axp)

axp2=divider.append_axes("top", size=1, pad=0)
axp2.plot(m,p)
axp2.set_yscale("log")
axp2.set_ylim([100,300])

axp2.spines['bottom'].set_visible(False)
axp2.xaxis.set_ticks_position('top')
axp2.yaxis.set_ticks_position('right')
pl.setp(axp2.get_xticklabels(), visible=False)

pl.show()

enter image description here

Tomislav
  • 31
  • 3

0 Answers0