Potential Bug in matplotlib ax.loglog() and downstream functions?

Viewed 37

Im trying to create some graphs which have lots of points towards smaller magnitudes, and covers numbers from 10^5 to about 10^8, so of course im trying to make the axis have a log scale to better see all the points and their distribution.

My problem is, when i use ax.loglog() i dont get consistent behaviour, i.e. other axes have their scale changed. (I have also tried downstream functions like ax.semilogx/semilogy, and ax.set_xscale/set_yscale which have the same behaviour)

Heres an example: Here i plot a bunch of points using sns.scatterplot, and i also plot a lineplot over the top (simply y=x). I plot 4 subplots of this data, and try to set each axis to log scale right after I plot it.

fig, ax = plt.subplots(1,4,sharey=True, figsize=(15,6), sharex=True)


y=variable_to_predict # called NSW Equivalent Emissions
xs=["Prediction (TOBD)","Prediction (TOBD,Sep/TOBD)","Prediction (TOBD,Sep/TOBD,ED/TOBD)","Prediction (TOBD,Sep/TOBD,ED/TOBD,Peer)"]

for i, x in enumerate(xs):
    maxval = 10**8
    sns.lineplot(x=np.linspace(ED_DATA[y].min(),maxval,10),y=np.linspace(ED_DATA[y].min(),maxval,10),
                 color="red", ax=ax[i])

    sns.scatterplot(data=ED_DATA,y=y,x=x,
                    hue = "State", alpha=0.7, linewidth=0, ax=ax[i])
    
    ax[i].loglog()

plt.show()

All graphs meant to have changed to loglog, but theres something weird going on

Whats interesting is, if i only set the last plot/axes to loglog scale, it actually changes all of them in a way that I want! see code below:

fig, ax = plt.subplots(1,4,sharey=True, figsize=(15,6), sharex=True)


y=variable_to_predict # called NSW Equivalent Emissions
xs=["Prediction (TOBD)","Prediction (TOBD,Sep/TOBD)","Prediction (TOBD,Sep/TOBD,ED/TOBD)","Prediction (TOBD,Sep/TOBD,ED/TOBD,Peer)"]

for i, x in enumerate(xs):
    maxval = 10**8
    sns.lineplot(x=np.linspace(ED_DATA[y].min(),maxval,10),y=np.linspace(ED_DATA[y].min(),maxval,10),
                 color="red", ax=ax[i])

    sns.scatterplot(data=ED_DATA,y=y,x=x,
                    hue = "State", alpha=0.7, linewidth=0, ax=ax[i])
    
ax[3].loglog()

plt.show()

All graphs changed scale, but i only changed the last one?

If anyone has any insight into whats actually happening in the depths of the matplotlib library I would be very appreciative if you could explain it. I have absolutely no clue, and now, no understanding on how to use the loglog() and related functions the right way.

Thanks in advance!

0 Answers
Related