Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Python] Custom tick marks in matplotlib

  1. Mar 25, 2015 #1
    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.
     
  2. jcsd
  3. Mar 25, 2015 #2

    phyzguy

    User Avatar
    Science Advisor

    The command is set_xticks([x1,x2,x3]). The list can have the ticks wherever you want. Here is some code:

    Code (Text):

    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()
     
     
  4. Mar 28, 2015 #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?
     
  5. Mar 28, 2015 #4

    phyzguy

    User Avatar
    Science Advisor

    Sure, you just need to apply the command at the subplot level, like this:
    Code (Text):

    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()
     
     
  6. Mar 29, 2015 #5
    Ah that has solved all my problems. Thanks very much!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Python] Custom tick marks in matplotlib
  1. Is Python the future? (Replies: 10)

  2. Python installation (Replies: 10)

  3. Strings in python (Replies: 15)

Loading...