[Python] Custom tick marks in matplotlib

In summary, the conversation discusses the use of matplotlib to add a specific tick on the x-axis to label a particular value. The command "set_xticks" can be used to add these ticks at any desired location. However, when using subplots, the command must be applied at the subplot level.
  • #1
sk1105
88
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
  • #2
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()
 
  • #3
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?
 
  • #4
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()
 
  • #5
Ah that has solved all my problems. Thanks very much!
 

Related to [Python] Custom tick marks in matplotlib

1. How do I change the tick marks in a matplotlib graph?

To change the tick marks in a matplotlib graph, you can use the set_xticks() and set_yticks() methods. These methods take in an array of values that you want to use as tick marks.

2. Can I customize the appearance of my tick marks in matplotlib?

Yes, you can customize the appearance of your tick marks in matplotlib. You can use the tick_params() method to change the size, color, and style of your tick marks.

3. How do I set a different interval for my tick marks in matplotlib?

You can set a different interval for your tick marks in matplotlib by using the set_xticks() and set_yticks() methods. These methods allow you to specify the tick mark values and the interval between them.

4. Can I add custom labels to my tick marks in matplotlib?

Yes, you can add custom labels to your tick marks in matplotlib. You can use the set_xticklabels() and set_yticklabels() methods to provide a list of labels for each tick mark value.

5. Is it possible to have non-numerical tick marks in matplotlib?

Yes, it is possible to have non-numerical tick marks in matplotlib. You can use the set_xticklabels() and set_yticklabels() methods to provide a list of labels for each tick mark value, even if they are not numerical values.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
916
  • Programming and Computer Science
Replies
2
Views
9K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
884
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top