FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Setting minor ticks with SecondaryAxis · Issue #16390 · matplotlib/matplotlib · GitHub

Repository navigation

Setting minor ticks with SecondaryAxis #16390

Closed

Description

Bug report

Bug summary

Hello, I am unable to set minor ticks without labels using SecondaryAxis. Using SecondaryAxis.set_ticks(ticks, minor=True) only makes major tick marks.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator

def forw(x):
    return x/5.2

def rev(x):
    return x*5.2

p_xax = [i for i in range(0,21,5)]
p_yax = [i for i in range(0,13,3)]
plt.scatter(p_xax, p_yax, color='black')
plt.xlabel("Main xaxis")
plt.ylabel("Main yaxis")
plt.xlim(0,20)
plt.ylim(0,15)
plt.xticks(np.arange(0,21,5))
plt.yticks(np.arange(0,16,3))

ax = plt.gca()
ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))

sec = ax.secondary_xaxis('top', functions=(forw, rev))
sec.set_xlabel(r"Secondary xaxis")
sec.set_ticks(np.arange(0,3.5,1), minor=True)
plt.savefig("test_plot.png")

Actual outcome

Expected outcome

I would like to have the "top xaxis" look like the bottom one, with minor ticks between the major ticks without labels. The plot shown makes minor ticks have the same size as the major ones, and I'm not sure how to turn off labels in the current code.

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 3.1.1 from conda
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.6.9

Activity

  1. jklymak commented on Feb 2, 2020

    Member

    You didn't set the major ticks to be the other ticks....

    sec.set_ticks(np.arange(0,3.5,1))
    sec.set_ticks(np.arange(0.5,3.5,1), minor=True)
    

    works for me.

  2. jstoppelman commented on Feb 2, 2020

    Author

    Here is my updated code with your suggestion:

    #!/usr/bin/env python
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.ticker import AutoMinorLocator
    import matplotlib
    
    def forw(x):
        return x/5.2
    
    def rev(x):
        return x*5.2
    print(matplotlib.get_backend())
    p_xax = [i for i in range(0,21,5)]
    p_yax = [i for i in range(0,13,3)]
    plt.scatter(p_xax, p_yax, color='black')
    plt.xlabel("Main xaxis")
    plt.ylabel("Main yaxis")
    plt.xlim(0,20)
    plt.ylim(0,15)
    plt.xticks(np.arange(0,21,5))
    plt.yticks(np.arange(0,16,3))
    
    ax = plt.gca()
    ax.xaxis.set_minor_locator(AutoMinorLocator(2))
    ax.yaxis.set_minor_locator(AutoMinorLocator(2))
    
    sec = ax.secondary_xaxis('top', functions=(forw, rev))
    sec.set_xlabel(r"Secondary xaxis")
    sec.set_ticks(np.arange(0,3.5,1))
    sec.set_ticks(np.arange(0.5,3.5,1), minor=True)
    plt.savefig("test_plot.png")
    

    Outcome

    I now get this figure, which doesn't have any minor tick marks at all. If it does work for you, then what version of matplotlib are you using?

  3. ImportanceOfBeingErnest commented on Feb 2, 2020

    Member

    Yes, this only works with master, or at least not with 3.1.0.

  4. jklymak commented on Feb 2, 2020

    Member
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.ticker import AutoMinorLocator
    
    def forw(x):
        return x/5.2
    
    def rev(x):
        return x*5.2
    
    p_xax = [i for i in range(0,21,5)]
    p_yax = [i for i in range(0,13,3)]
    plt.scatter(p_xax, p_yax, color='black')
    plt.xlabel("Main xaxis")
    plt.ylabel("Main yaxis")
    plt.xlim(0,20)
    plt.ylim(0,15)
    plt.xticks(np.arange(0,21,5))
    plt.yticks(np.arange(0,16,3))
    
    ax = plt.gca()
    ax.xaxis.set_minor_locator(AutoMinorLocator(2))
    ax.yaxis.set_minor_locator(AutoMinorLocator(2))
    
    sec = ax.secondary_xaxis('top', functions=(forw, rev))
    sec.set_xlabel(r"Secondary xaxis")
    sec.set_ticks(np.arange(0,3.5,1))
    sec.set_ticks(np.arange(0.5,3.5,1), minor=True)
    plt.show()

    This is on master, so maybe something got fixed.

  5. jklymak commented on Feb 2, 2020

    Member

    Yes, this should be fixed 3.2: #14447

  6. jstoppelman commented on Feb 2, 2020

    Author

    Ok thank you, I've cloned master and am getting the same behavior as you do. I will close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL