# Change the y-axis ticks and lables

• Python
How can I change the y-axis ticks and labels in Python? I also want to display the grid at each shown label. When I used the following code, I change the ticks and labels, but it also keeps the default y labels. How to remove the default ticks and labels, and create new ones?

Python:
fig, ax = plt.subplots()
ax.plot(x, y)
start, end = ax.get_ylim()
ax.set_yticks(start, end, stepsize)
plt.grid(True)

jedishrfu
Mentor
This isn’t a python problem, you need to find the api description for the plot library you are using and see what methods you can call.

Sometimes it may be a simple method call, at other times you might have to get an attribute managed by the plot instance and change things in it using its api.

What do you mean it isn't a Python problem? The visualization library I use is a Python library. At the end Python is a collection of libraries, isn't it? Anyway, I did read the documentation, but as I said in my original post, some default labels stayed. I am not sure why?!! To be more specific, when I plot the data I have, I get 1, 0.6, and 0.4 as the default y labels. When I try to set new ticks and labels, I get the new ones, but the original 1, 0.6, and 0.4 stay. So, I will have, for example, 0.4 (the original) and 0.41 (the new) as labels. I don't want that. I want to remove the original labels, and define new ones.

(I'm assuming you're using the standard Pylab and not Matplotlib or something.) I have a note that indicates one needs to specify the variable values when you first call the functions... otherwise the default values are used.

I use matplotlib.pyplot actually. Could you give a code example?

I don't know if the context is the same; this was not used in interactive/interpretative mode, I used a canvas, etc...
I tried to pull out what's relevant, so this is "as is":

e.g.:
IE = Figure(figsize = (8, 6), dpi = 100, facecolor = (0, 0.48, 0.48))
b.plot(I had used x and y arrays here)
b.tick_params(axis = 'x', labelsize = 9)
b.tick_params(axis = 'y', labelsize = 9)
ypos = b.set_ylabel('[myYLabel]', labelpad = 17)
ypos.set_rotation(0)
...​

(There are a bunch of matplotlib guides around; the frustrating thing can be that there are so many versions to sort through.)

I don't know if the context is the same; this was not used in interactive/interpretative mode, I used a canvas, etc...
I tried to pull out what's relevant, so this is "as is":

e.g.:
IE = Figure(figsize = (8, 6), dpi = 100, facecolor = (0, 0.48, 0.48))
b.plot(I had used x and y arrays here)
b.tick_params(axis = 'x', labelsize = 9)
b.tick_params(axis = 'y', labelsize = 9)
ypos = b.set_ylabel('[myYLabel]', labelpad = 17)
ypos.set_rotation(0)
...​

Did not work out. I still have the problem.

Did not work out. I still have the problem.

I followed up with:
IE.canvas.draw()
IE.canvas.flush_events()​

...which may not be relevant in your setting. I'm hesitant to recommend too much, my thing was put together with curve fit plot and also in the context of a near-real-time display that was updating....

Gotta go, but if nobody steps up in the meantime I'll try to refresh my brain tomorrow....

Did not work out. I still have the problem.

Just one last intuitive guess:

If you're still using get_ylim()... I have this bad recollection in my head of that not accomplishing what one thinks it does--relative to defaults.

Yes, I removed get_ylim. I used min(y), and max(y) instead. Probably, it is better to have the complete code. It is as follows:

Python:
x = [ 0.31622777, 0.39810717,  0.50118723,  0.63095734,  0.79432823,  1.,
1.25892541,  1.58489319,  1.99526231,  2.51188643,  3.16227766,  3.98107171,
5.01187234,  6.30957344,  7.94328235, 10.]
y1 = [0.9966210571201278, 0.9939275573981057, 0.9893273095470798,
0.9817209857904535, 0.9696055811448635, 0.9511095395405945,
0.9241765948376699, 0.886933020223576, 0.8381987809492316,
0.778001718612356, 0.7078864077246305, 0.6308436561578836,
0.5508331392752104, 0.47205559940799435, 0.39823328460749174,
0.3321230094947829]
y2 = [0.9956, 0.9932, 0.9888, 0.9818, 0.9686, 0.9483, 0.9222, 0.8859,
0.8397, 0.7799, 0.7130000000000001, 0.6366, 0.5439, 0.4665,
0.38880000000000003, 0.3195]

fig, ax = plt.subplots(figsize = (10, 8))
ax.set_yscale('log')
ax.set_yticks(np.arange(min(y), max(y), 0.1))
ax.set_ylabel(np.arange(min(y), max(y), 0.1))
ax.plot(x, y1, 'r-*', label = 'curve1')
ax.plot(x, y2, 'k-o', label = 'curve2')
ax.legend()
ax.grid(True)
plt.show()

and the figure is attached. See, the old ticks and labels remain!! Actually, in this case the ticks are fine, but the grid doesn't happen on the default ticks (see fig1), so, I tried to change them, which resulted in this issue of multiple ticks and labels. My original problem was to draw the grid at each y tick.

#### Attachments

• fig.pdf
11.5 KB · Views: 153
• fig1.pdf
9.1 KB · Views: 147
OK, I think I know what the problem is. When I remove ax.set_yscale('log'), I get what I want. So, I need to set the ticks and labels, and then use ax.set_yscale('log'). This resulted in the attached figure. The code

Python:
x = [ 0.31622777, 0.39810717,  0.50118723,  0.63095734,  0.79432823,  1.,
1.25892541,  1.58489319,  1.99526231,  2.51188643,  3.16227766,  3.98107171,
5.01187234,  6.30957344,  7.94328235, 10.]
y1 = [0.9966210571201278, 0.9939275573981057, 0.9893273095470798,
0.9817209857904535, 0.9696055811448635, 0.9511095395405945,
0.9241765948376699, 0.886933020223576, 0.8381987809492316,
0.778001718612356, 0.7078864077246305, 0.6308436561578836,
0.5508331392752104, 0.47205559940799435, 0.39823328460749174,
0.3321230094947829]
y2 = [0.9956, 0.9932, 0.9888, 0.9818, 0.9686, 0.9483, 0.9222, 0.8859,
0.8397, 0.7799, 0.7130000000000001, 0.6366, 0.5439, 0.4665,
0.38880000000000003, 0.3195]

fig, ax = plt.subplots(figsize = (10, 8))

major_ticks = np.arange(0.1, 1.1, 0.1)
minor_ticks = np.arange(0.1, 1.1, 0.1)

ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)

ax.set_yscale('log')

ax.grid(which='minor', alpha=0.2)
ax.grid(which='major', alpha=0.5)
ax.plot(x, y1, 'r-*', label = 'curve1')
ax.plot(x, y2, 'k-o', label = 'curve2')
ax.legend()
#ax.grid(True)
plt.show()
fig.savefig("fig1.pdf", bbox_inches='tight')

#### Attachments

• fig1.pdf
9.3 KB · Views: 147
OK, I think I know what the problem is. When I remove ax.set_yscale('log'), I get what I want. So, I need to set the ticks and labels, and then use ax.set_yscale('log'). This resulted in the attached figure.

Oh, okay!