0

I am plotting data using the following code:

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.ticker import MultipleLocator, FormatStrFormatter



Ed={"vCs":-5811.63801749,"vSb":-5352.588077, "vCl":-5825.86517}


Eprist=-5793.09448
dCs=np.array([0.0,-0.41613239])
uCs=-46.77765971+dCs
dSb=np.array([0.0,-0.624198585])
uSb=-505.785546+dSb
dCl=np.array([0.0,-0.1387107967])
uCl=-32.79123908+dCl
dO2=np.array([0.0,0.5792])
uO2=-66.07463153+dO2
uO2=-66.07463153

EfCl_Cl=Ed["vCl"]-Eprist+uCl-uO2
EfSb_Cl=Ed["vSb"]-Eprist+uSb-uO2
EfCs_Cl=Ed["vCs"]-Eprist+uCs-uO2
EfCl=np.concatenate((EfCl_Cl,EfSb_Cl,EfCs_Cl))


fig, (ax1,ax2,ax3) = plt.subplots(1,3)

ax1.plot(dCs,EfCs_Cl,label="vCs", 
         color="black",linestyle="-.", marker='s')
ax2.plot(dSb,EfSb_Cl,label="vSb",
         color="red",linestyle="--", marker='^')
ax3.plot(dCl,EfCl_Cl,label="vCl",
         color="blue",linestyle=":", marker='o')
ax1.legend()




xlims=[[-0.7,0.05],[-0.7,0.05],[-0.3,0.05]]
xticks=[np.arange(-0.5,0.1,0.2),np.arange(-0.6,0.1,0.2),np.arange(0.0,-0.3,-0.1)]
minorxLocator   = MultipleLocator(0.05)
minoryLocator   = MultipleLocator(0.05)

i=0
for ax in (ax1,ax2): 
    ax.set_ylim([min(EfCl)-0.1,max(EfCl)+0.1])
    ax.set_xlim(xlims[i])
    ax.set_xticks(xticks[i])
    ax.xaxis.set_minor_locator(minorxLocator)
    ax.yaxis.set_minor_locator(minoryLocator)
    i += 1

ax3.set_xlim(xlims[i])
ax3.set_xticks(xticks[i])
ax3.xaxis.set_minor_locator(minorxLocator)
ax3.yaxis.set_minor_locator(minoryLocator)

plt.show() 

and I get the graph with the minor ticks on the axes 1 and 2 only up to the range in ax3. Graph with minor ticks wrong in axes 1 and 2

However if I comment the line: ax3.xaxis.set_minor_locator(minorxLocator) I get the minor ticks correct on axes 1 and 2 (but obviously no minor ticks in axes 3) as shown in fig:

Minor ticks correct on axes 1 and 2

How do I get minor ticks right on all axes as it should?? I am using Spyder to plot (although on terminal yielded same result, and version 3.7.6 python)

  • What do you mean? Can I set minor ticks in general for all graphs? – Rogério Gouvêa Feb 12 '21 at 03:42
  • Don't reuse the same locator. Also noted in the [docs](https://matplotlib.org/3.3.3/api/ticker_api.html#matplotlib.ticker.Locator). – BigBen Feb 12 '21 at 03:42
  • Read the linked answer. "This is because you're sharing the same locator object between multiple y-axis objects." – user202729 Feb 12 '21 at 03:42
  • YES, problem solved. I used the line ax.xaxis.set(minor_locator=MultipleLocator(0.05)) instead of .set_minor_locator, as suggested in the linked question. Thank you!! – Rogério Gouvêa Feb 12 '21 at 03:50

0 Answers0