Skip to content

Suspected incorrect axis transformation of symlog #7008

Closed as not planned
Closed as not planned

Description

@desert0616

Hi, @mdboom.
Can you roughly explain the meaning of _linscale_adj in SymmetricalLogTransform which was introduced via d8bf0fed4d62324489ffe1d21d114c87ae9ca6a4?


class SymmetricalLogTransform(Transform):
    input_dims = 1
    output_dims = 1
    is_separable = True
    has_inverse = True

    def __init__(self, base, linthresh, linscale):
        Transform.__init__(self)
        self.base = base
        self.linthresh = linthresh
        self.linscale = linscale
        self._linscale_adj = (linscale / (1.0 - self.base ** -1))
        self._log_base = np.log(base)

I am quite puzzled by it.

According to the documentation about linscalex / linscaley, its value is the number of decades to use for each half of the linear range. For example, when linscale == 1.0 (the default), the space used for the positive and negative halves of the linear range will be equal to one decade in the logarithmic range.

As I understand it, it means that when the base is 10 and the linthresh equals to 1, the length of the interval between 0 and 1 should equal to the interval between 1(10^0) and 10(10^1). And this feature seems to be very useful that user can adjust the relative ratio between linear range and logarithmic range.

However, since the _linscale_adj amplifies the original linscale by 1/(1-1/base), the actual place for the linear range will be 1.11x compared to logarithmic range per decade.

For example,


import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 5, 10, 100, 1000])
y = np.array([0] * 6)

plt.scatter(x, y)
plt.xscale('symlog', linthreshx=10, basex=10, linscalex=1)
plt.grid(True)
plt.show()

base10

If the base is set to 2, the linear range will be 2x than logarithmic range per decade and this issue may be more considerable:


import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 0.5, 1, 2, 4, 8, 16])
y = np.array([0] * 7)

plt.scatter(x, y)
plt.xscale('symlog', linthreshx=2, basex=2, linscalex=1)
plt.grid(True)
plt.show()

base2

Since _linscale_adj is relatively independent and is only used to be a coefficient of transformation for symlog, it seems not to be a bug. So, what is the meaning of the scaling of _linscale_adj which related to its base?

Well, as a user, the figures showed above is very weird and I used to set the linscale manually to 0.9 (base=10) to make each tick interval to be equal.

Thanks.

Activity

  1. added this to the milestone on Aug 30, 2016
  2. desert0616 commented on Sep 24, 2016

    @desert0616
    Author

    is any comment for this issue?

  3. tacaswell commented on Sep 24, 2016

    @tacaswell
    Member

    @phobson You think a lot about non-linear scales, do you have any insight into this?

  4. phobson commented on Sep 27, 2016

    @phobson
    Member

    I'm still catching up on paid gig stuff after my vacation. But I'll shoot to give this a good look later in the week.

  5. modified the milestones: , on Oct 3, 2017
  6. jgehrcke commented on Jun 26, 2019

    @jgehrcke
    Contributor

    I think this should get more attention. I cannot submit a bug report right now, I don't have enough clarity, but I face a quite similar issue with symlog, where the tick intervals are not equidistant.

  7. phobson commented on Jun 26, 2019

    @phobson
    Member

    @jgehrcke could you add a small example that illustrates what you're describing to this issue?

  8. jgehrcke commented on Jun 26, 2019

    @jgehrcke
    Contributor

    @jgehrcke could you add a small example that illustrates what you're describing to this issue?

    Thanks for asking :)

    $ cat repro.py 
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(1,2,10)
    y = np.array([0, 3, 10, 20, 30, 40, 100, 400, 800, 1000])
    
    plt.figure()
    plt.yscale('symlog')
    plt.plot(x,y)
    
    plt.show()
    

    Figure_1

    I would expect the distance between the 10^0 tick and the 10^1 tick to be equal to the distance between the 10^1 and the 10^2 tick (equidistant major tick marks are the goal by default, right?).

    However, the distance between the 10^0 tick and the 10^1 tick is larger. That is unusual.

    >>> mpl.__version__
    '3.1.0'
    
  9. desert0616 commented on Jun 26, 2019

    @desert0616
    Author

    wow... amazing, this thread is still alive after 3 years...

  10. jklymak commented on Jun 26, 2019

    @jklymak
    Member

    linthreshx and linthreshy default to 2. Try plt.yscale('symlog', linthreshy=1)

  11. phobson commented on Jun 26, 2019

    @phobson
    Member

    This is fascinating

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(1,2,10)
    y = np.array([0, 3, 10, 20, 30, 40, 100, 400, 800, 1000])
    
    fig, axes = plt.subplots(ncols=2)
    for thresh, ax in enumerate(axes, 1): 
        ax.set_yscale("symlog", linthreshy=thresh)
        ax.plot(x,y)
        ax.set_title(f"linthreshy = {thresh}")
    
    plt.show()

    image

  12. jklymak commented on Jun 26, 2019

    @jklymak
    Member

    But that's correct...

  13. phobson commented on Jun 26, 2019

    @phobson
    Member

    Oh yeah, I had brain farted on 10^0 = 1

  14. phobson commented on Jun 26, 2019

    @phobson
    Member

    so the gap between 1 and 0 changing makes sense and same for the game between 1 and 10

  15. jgehrcke commented on Jun 26, 2019

    @jgehrcke
    Contributor

    linthreshx and linthreshy default to 2.

    Dang! I assumed it's 1. Not documented here: https://matplotlib.org/3.1.0/api/scale_api.html#matplotlib.scale.SymmetricalLogScale

    But interestingly the "Defaults to 2" has very recently been added here: 2679354#diff-9e3c26547d61ec9447f5420ec32f08f4R549 -- so I suppose with the next release the documentation will be clearer.

    Thanks! So, "my issue" here is resolved. Lovely.

  16. jklymak commented on Jun 26, 2019

    @jklymak
    Member

    @jgehrcke good point. that should be fixed. Any appetite for making a PR to do that?

  17. timhoffm commented on Jun 29, 2019

    @timhoffm
    Member
  18. gregcolyer commented on Jul 19, 2019

    @gregcolyer

    Even in @phobson's linthreshy=1 plot above, there is still a different gap between 0 and 1 vs. 1 and 10, presumably due to the (1-1/b) factor mentioned originally by @desert0616 at the top. Cf. issue #10369, where I suggested what to change in the code and/or documentation to address this: for the examples there I also set linthreshy=1, so I'm not sure if I thought about other values (or if that is independent).

  19. YannDubs commented on Mar 6, 2020

    @YannDubs

    putting together what @gregcolyer and @desert0616 said (and code in #10369). There are 2 current workarounds depending on what you want (equidistant or smooth):

    import matplotlib.pyplot as plt
    import numpy as np
    
    # Define some curves:
    x = np.linspace(0.1,3.,100)
    y1 = x
    y2 = x**2
    y3 = np.exp(x)/2.
    y4 = np.log(x)*2.
    
    def generate_plot(get_kwargs, ax, base, sffx):
        ax.set_title(f'base {base} {sffx}')
        
        if get_kwargs == "log":
            ax.set_ylim(.01,100)  
            ax.set_yscale('log', basey=base)
        else:
            ax.set_ylim(-100,100)  
            ax.set_yscale('symlog',linthreshy=1, **get_kwargs(base)) 
            
        ax.plot(x,y1)
        ax.plot(x,y2)
        ax.plot(x,y3)
        ax.plot(x,y4)
        
    
    def generate_plots(get_kwargs, sffx):
        bases = [2,5,10,100]
        
        f, axes = plt.subplots(1,len(bases),figsize=(len(bases)*5,4))
        
        for base, ax in zip(bases, axes):
            generate_plot(get_kwargs, ax, base, sffx)
    # log plot (see: the plot is smooth)
    get_kwargs_dflt = lambda base : dict(basey=base, subsy=list(range(base)))
    generate_plots("log", "log plot")

    logplot

    # Default parameters (not equidistant and not smooth)
    get_kwargs_dflt = lambda base : dict(basey=base, subsy=list(range(base)))
    generate_plots(get_kwargs_dflt, "symlog dflt")

    symlogdflt

    # Equidistant ticks
    get_kwargs_unif = lambda base : dict(basey=base,linscaley=(1-(1/base)), subsy=list(range(base)))
    generate_plots(get_kwargs_unif, "symlog equidistant")

    equidistant

    # Keep smooth plots (don't show inexistant "kinks")
    get_kwargs_smooth = lambda base : dict(basey=base,linscaley=np.log(np.e)/np.log(base) * (1-(1/base)), subsy=list(range(base)))
    generate_plots(get_kwargs_smooth, "symlog smooth")

    smooth

  20. QuLogic commented on Mar 11, 2020

    @QuLogic
    Member

    @YannDubs That seems like a pretty good example (or maybe tutorial), would you like to make a PR adding it? It would need some amount of prose added to explain what it does. It could supplement examples/scales/symlog_demo.py.

  21. github-actions commented on Mar 28, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  22. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on May 30, 2023
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

    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions