Skip to content

Fix limits if one axis scale is 'log' #27085

@swagner-astro

Description

@swagner-astro

Problem

I am plotting my data (x,y) and want to show the x-axis in logscale. Because there are some negative values in x, the log10 can not be computed for all. This results in nans which are simply omitted when plotting (and this is what I want) but the y-axis limits are not updated for these missing values. It would be helpful to automatically set new axis limits if there are nans in the data.

Example:

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10, 11, 1) 
plt.plot(x,x, marker='d')

Screen Shot 2023-10-11 at 20 40 14

and when I add the log scale on the x-axis, the negative values are omitted but the y-axis is not updated (even though I set nonpositive to 'clip' -- so maybe this is a bug?)

x = np.arange(-10, 11, 1) 
plt.plot(x,x, marker='d')
plt.xscale('log', nonpositive='clip')

Screen Shot 2023-10-13 at 15 53 02

In order to manually set ylimone would need to know the exact values of the data, so I suggest this could be adjusted automatically.

Setup

Python 3.8.8
Matplotlib 3.7.1
Numpy 1.24.3
notebook 6.2.0 (executing and plotting in jupyter notebook)
on macOS 12.6.2 (Monterey)

Proposed solution

Idea:
If the xscale is set to 'log' and there are negative values in x, take the position of minimum of x < 0 and set y value at that position as new limit (ideally with the same margin as in the rest of the figure):

assuming self is the axis

margin = 0.1
if len(x[x<0]) !=0 and self.get_xscale=='log':
    ii_x_g0 = np.min(np.where(x>0))
    self.set_ylim(bottom = y[ii_x_g0] - y[ii_x_g0] * margin)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy