Change the y-axis ticks and lables

  • Python
  • Thread starter EngWiPy
  • Start date
  • Tags
    Change
In summary: 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,
  • #1
EngWiPy
1,368
61
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)
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
Bear with me here, haven't thought about this for a bit and I'm looking at some notes I had.

(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.
 
  • #5
I use matplotlib.pyplot actually. Could you give a code example?
 
  • #6
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 = IE.add_subplot(111)
b.plot(I had used x and y arrays here)
b.tick_params(axis = 'x', labelsize = 9)
b.tick_params(axis = 'y', labelsize = 9)
b.set_xlabel('[myXLabel]', labelpad = 8)
ypos = b.set_ylabel('[myYLabel]', labelpad = 17)
ypos.set_rotation(0)
...​
 
  • #7
(There are a bunch of matplotlib guides around; the frustrating thing can be that there are so many versions to sort through.)
 
  • #8
SunThief said:
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 = IE.add_subplot(111)
b.plot(I had used x and y arrays here)
b.tick_params(axis = 'x', labelsize = 9)
b.tick_params(axis = 'y', labelsize = 9)
b.set_xlabel('[myXLabel]', labelpad = 8)
ypos = b.set_ylabel('[myYLabel]', labelpad = 17)
ypos.set_rotation(0)
...​

Did not work out. I still have the problem.
 
  • #9
EngWiPy said:
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...
 
  • #10
EngWiPy said:
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.
 
  • #11
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: 219
  • fig1.pdf
    9.1 KB · Views: 206
  • #12
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: 196
  • #13
EngWiPy said:
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!
 

Related to Change the y-axis ticks and lables

1. How do I change the y-axis ticks and labels in my graph?

To change the y-axis ticks and labels in a graph, you will need to access the settings or options for the specific graphing tool or software you are using. Look for options to customize the axes or labels, and you should be able to manually enter the desired values or adjust the tick increments.

2. Can I change the y-axis ticks and labels to display specific values?

Yes, most graphing tools and software allow you to customize the y-axis ticks and labels to display specific values. This can be useful if you want to highlight certain data points or make the graph more visually appealing.

3. Is it possible to change the format of the y-axis labels?

Yes, you can change the format of the y-axis labels to display different units, symbols, or decimal places. This can be done by accessing the label or axis settings and selecting the desired format.

4. How do I adjust the spacing between y-axis ticks?

To adjust the spacing between y-axis ticks, you will need to access the settings for the specific graphing tool or software you are using. Look for options to customize the axes or labels, and you should be able to manually adjust the tick increments to increase or decrease the spacing.

5. Can I change the orientation of the y-axis ticks and labels?

Yes, you can change the orientation of the y-axis ticks and labels to be horizontal or vertical. This can be done by accessing the settings for the specific graphing tool or software you are using and selecting the desired orientation option.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
948
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
966
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
30
Views
4K
Back
Top