Search code examples
pythonmatplotlibsubplot

A way to prevent matplotlib to remove the numbers on the axis


I am plotting a sample in 16 subplots together with one value in each of the plots, like this: (example of first subplot)

fig, ax = plt.subplots(4, 4)
ax[0, 0].scatter(C_CH_metalicities,C_CH_X_over_Fe, color = "gray", s = 3) #row=0, col=0 (100s of values)
ax[0,0].scatter(J072022_met,J072022_dict["C H"], marker = "*", s = 200) #(1 value)

Before the addition of the single data value, my plot looks like this:Nice values along the yaxis And after like this:Not nice values along the yaxis The problem is the values on the yaxis. I want it to remain the same as it was before I added the single value.

I tried manually setting the yticks like:

ax[0,0].set_yticks([-1,0,1,2,3])

But this only gives a "-" in the positions of the yvalues, but does not show the number.

I know this is a very boring question, but it is doing my head in.


Solution

  • The first argument of ax.set_yticks() is where the tick locations are, but not how they are labeled. You just need to add in the labels parameter.

    ax[0,0].set_yticks([-1,0,1,2,3], labels = [-1,0,1,2,3])