Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
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 npimport matplotlib.pyplot as pltx = np.arange(-10, 11, 1) plt.plot(x,x, marker='d')
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')
In order to manually setylim
one 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.1if 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)