[Python] Custom tick marks in matplotlib

Click For Summary

Discussion Overview

The discussion revolves around customizing tick marks in matplotlib, specifically how to add a specific tick mark to the x-axis in addition to regular ticks. The context includes a graph of the electron-positron annihilation cross-section as a function of collider energy, with a focus on marking the mass of the Z boson.

Discussion Character

  • Technical explanation, Homework-related

Main Points Raised

  • One participant seeks a method to add a specific tick mark on the x-axis while maintaining regular ticks.
  • Another participant suggests using the command set_xticks([x1,x2,x3]) to specify tick locations, providing a code example.
  • A participant expresses concern that using set_xticks disrupts the layout of their subplots, resulting in only one subplot being displayed.
  • A later reply indicates that the set_xticks command should be applied at the subplot level to avoid layout issues, providing a revised code example.
  • One participant confirms that the suggested solution resolves their issues.

Areas of Agreement / Disagreement

Participants generally agree on the method to set custom ticks, but there is a specific concern regarding the impact on subplot layouts, which remains unresolved for some users.

Contextual Notes

There may be limitations related to the specific implementation of subplots and the order of commands that could affect the display of multiple plots.

sk1105
Messages
88
Reaction score
12
I've looked around for this and so far haven't found anything close enough to what I'm after.

In matplotlib, I want to get a specific tick on the x-axis to label a particular value. I know how to use axvline to get a vertical line marking that point, which is good, and I know how to add annotations onto the graph itself, but is there a way of getting an additional tick mark at that particular x value, in addition to the regular marks?

To add some context, I have a graph of the electron-positron annihilation cross-section as a function of collider energy, and I want to mark the mass of the Z boson to point out the resonant behaviour.
 
Technology news on Phys.org
The command is set_xticks([x1,x2,x3]). The list can have the ticks wherever you want. Here is some code:

Code:
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[1,4,9,16,25]

plt.figure()
ax1 = plt.axes()
ax1.plot(x,y)
ax1.set_xticks([0.0,0.37, 1.85, 4.23])
plt.show()
 
Thanks, that does what I want...as well as something I don't want. It completely ruins my subplots. I had 4 plots on a 2x2 grid, and adding that command causes it to show only my fourth subplot, as a full-window figure. I've tried putting the command in a few different places to no avail. Is there a special procedure when using this with subplots?
 
sk1105 said:
Thanks, that does what I want...as well as something I don't want. It completely ruins my subplots. I had 4 plots on a 2x2 grid, and adding that command causes it to show only my fourth subplot, as a full-window figure. I've tried putting the command in a few different places to no avail. Is there a special procedure when using this with subplots?

Sure, you just need to apply the command at the subplot level, like this:
Code:
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[1,4,9,16,25]

plt.figure()
ax1 = plt.subplot(2,2,1)
ax1.plot(x,y)
ax1.set_xticks([0.0,0.74, 1.85, 4.23])
ax2 = plt.subplot(2,2,2)
ax2.plot(x,y)
ax2.set_xticks([0.0,0.74, 1.85, 4.23])
ax3 = plt.subplot(2,2,3)
ax3.plot(x,y)
ax3.set_xticks([0.0,0.74, 1.85, 4.23])
ax4 = plt.subplot(2,2,4)
ax4.plot(x,y)
ax4.set_xticks([0.0,0.74, 1.85, 4.23])
plt.show()
 
Ah that has solved all my problems. Thanks very much!
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
14K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
10K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K