I'm generating a scatter plot in matplotlib. Everything works fine if I use linear scales.
But since I'm mainly interested in the lower values, I thought I'd use logarithmic scaling. However, even though I have set my x-axis limits explicitly to (0,1), the axis starts at 0.1, so i miss everything below that!
Why does the logarithmic axis not start at zero, and how can I force it to?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,1,100)
y = np.random.randint(1000, size=100)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x, y)
ax.set_xlim(0,1.2)
ax.set_ylim(0,1000)
ax.set_yscale('log')
ax.set_xscale('log')
ax.yaxis.set_major_formatter(plt.FormatStrFormatter('%1.0f'))
ax.xaxis.set_major_formatter(plt.FormatStrFormatter('%.1f'))
ax.xaxis.set_minor_formatter(plt.FormatStrFormatter('%.1f'))
# this red line at x = 0.1
ax.axvline(x=0.1,color='r')
plt.show()
Any help is greatly appreciated!
Usually logarithmic axes never start at zero because there is no "good" value for log(0) on the x-axis, because log(0)==x only for x->-infinity.
Related
A matplotlib secondary axis is reversed when plotting on a log scale.
In the attached example, I use magnitude as the primary (left) axis, luminosity as the secondary (right) axis. Higher luminosity should correspond to a smaller magnitude, as it does in the first two plots. However, in the third plot, when I use a log scale for luminosity, luminosity increases with magnitude, which is incorrect. Is this a bug, or am I doing something wrong?
# Test secondary axis
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
print(matplotlib.__version__)
# Mag to luminosity (solar units) conversion
M_sun = 4.83
def mag2lum(mag):
return 10**(0.4*(M_sun - mag))
def lum2mag(L):
return M_sun - 2.5*np.log10(L)
def mag2lgl(mag):
return 0.4*(M_sun - mag)
def lgl2mag(lgl):
return M_sun - 2.5*lgl
# log luminosity as second axis - correct behaviour
fig, ax = plt.subplots(constrained_layout=True)
plt.ylabel(r'$M_G$ [mag]')
plt.xlim(-1, 5)
plt.ylim(10, 0)
secax = ax.secondary_yaxis('right', functions=(mag2lgl, lgl2mag))
secax.set_ylabel(r'$\log\ L_G\ [L_\odot]$')
plt.show()
# luminosity as second axis - correct behaviour, but labelling is horrible
fig, ax = plt.subplots(constrained_layout=True)
plt.ylabel(r'$M_G$ [mag]')
plt.xlim(-1, 5)
plt.ylim(10, 0)
secax = ax.secondary_yaxis('right', functions=(mag2lum, lum2mag))
secax.set_ylabel(r'$L_G\ [L_\odot]$')
plt.show()
# luminosity as second axis on log scale: axis is reversed
fig, ax = plt.subplots(constrained_layout=True)
plt.ylabel(r'$M_G$ [mag]')
plt.xlim(-1, 5)
plt.ylim(10, 0)
secax = ax.secondary_yaxis('right', functions=(mag2lum, lum2mag))
secax.set_ylabel(r'$L_G\ [L_\odot]$')
secax.set_yscale('log')
plt.show()
log luminosity as second axis - correct behaviour
luminosity as second axis - correct behaviour, but labelling is horrible
luminosity as second axis on log scale: axis is reversed
In third plot, luminosity should increase upwards.
In the simple case of one y axis, you can indeed set the scale. However, note what it is happening here, in your third case. The "scale" is not a decision of the plot drawing, but it is a consequence of the functions that translate between the main y axis and the secondary axis. What I mean is that the values and their positions in the secondary axis are "locked", as they need to match the corresponding ones in the main axis. So what you need, is not to try to change the secondary axis, but its ticks and labels. For that, you can use custom tick locations labels, or, as done below, import pre-defined locators and formatters.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import LogLocator, LogFormatterMathtext
M_sun = 4.83
def mag2lum(mag):
return 10**(0.4*(M_sun - mag))
def lum2mag(L):
return M_sun - 2.5*np.log10(L)
fig, ax = plt.subplots(constrained_layout=True)
ax.set_ylabel(r'$M_G$ [mag]')
ax.set_xlim(-1, 5)
ax.set_ylim(10, 0)
secax = ax.secondary_yaxis('right', functions=(mag2lum, lum2mag))
secax.set_ylabel(r'$L_G\ [L_\odot]$')
# do this instead of trying to set scale
secax.yaxis.set_major_locator(LogLocator())
secax.yaxis.set_major_formatter(LogFormatterMathtext())
plt.show()
Is there an easy way to avoid pyplot zooming far into noisy data?
Something like a lower boundary for the axis limits.
I am not trying to set a fix boundary to my axis, as this will fully disable automatic scaling.
Maybe a "minimum tick distance" would also work.
Right now I am using an additional 'invisible' plot in my graph that will define the maximum zoom.
Some example that illustrates what I want to achieve:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100, 1)
noise = np.random.randn(len(x))*0.1
y = 10+noise
y_dummy_low = [0]*len(x)
y_dummy_high = [20]*len(x)
plt.figure()
plt.plot(x, y) # noise data i actually want to plot
plt.plot(x, y_dummy_low, y_dummy_high, marker="None", linestyle="None") # this will avoid zooming too much
plt.show()
Zooming too far
Zooming OK
Is there a way to automatically not display tick mark labels if they would protrude past the axis itself? For example, consider the following code
#!/usr/bin/python
import pylab as P, numpy as N, math as M
xvals=N.arange(-10,10,0.1)
yvals=[ M.sin(x) for x in xvals ]
P.plot( xvals, yvals )
P.show()
See how the -10 and 10 labels on the x-axis poke out to the left and right of the plot? And similar for the -1.0 and 1.0 labels on the y-axis. Can I automatically suppress plotting these but retain the ones that do not go outside the plot limits?
I think you could just format the axis ticks yourself and then prune the ones
that are hanging over. The recommended way to deal with setting up the axis is
to use the ticker API. So for example
from matplotlib.ticker import MaxNLocator
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
xvals=np.arange(-10,10,0.1)
yvals=[ np.sin(x) for x in xvals ]
ax.plot( xvals, yvals )
ax.xaxis.set_major_locator(MaxNLocator(prune='both'))
plt.show()
Here we are creating a figure and axes, plotting the data, and then setting the xaxis
major ticks. The formatter MaxNLocator is given the
argument prune='both' which is described in the docs here.
This is not exactly what you were asking for, but maybe it will solve your problem.
In a figure with 2x2 subplots, I need both the subplots on the right to share the x-axis, but the ones on the left not to share their axis. In addition, I need the subplot that is determining the x-axis limits to have 'equal' aspect ratio. I tried this:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2, figsize=(12, 9))
# Subplot [0,1]
ax[0,1].axis('equal')
ax[0,1].plot(...)
[xmin01, xmax01, ymin01, ymax01] = self.ax[0,1].axis()
# Subplot [1,1]
ax[1,1].plot(...)
ax[1,1].set_xlim(left=xmin01, right=xmax01)
This is not working: the limits of the x-axis returned by axis() are near the data limits and are not the real limits shown in the graphed subplot. Changing the position of ax[0,1].axis('equal') after the plot command has no effect. Any idea?
Looking into the pyplot source code I discovered that axis('equal') is calling the method set_aspect(). This latter method is modifying the variable self._aspect but it is not further updating anything related! Then, I looked for and found the method that is really updating the aspect ratio: it is named apply_aspect(). So, it doesn't seem very elegant, but at least my problem is solved as shown:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2, figsize=(12, 9))
# Subplot [0,1]
ax[0,1].axis('equal')
ax[0,1].plot(...)
ax[0,1].apply_aspect()
[xmin01, xmax01, ymin01, ymax01] = self.ax[0,1].axis()
# Subplot [1,1]
ax[1,1].plot(...)
ax[1,1].set_xlim(left=xmin01, right=xmax01)
I am trying to make a filled contour for a dataset. It should be fairly straightforward:
plt.contourf(x, y, z, label = 'blah', cm = matplotlib.cm.RdBu)
However, what do I do if my dataset is not symmetric about 0? Let's say I want to go from blue (negative values) to 0 (white), to red (positive values). If my dataset goes from -8 to 3, then the white part of the color bar, which should be at 0, is in fact slightly negative. Is there some way to shift the color bar?
First off, there's more than one way to do this.
Pass an instance of DivergingNorm as the norm kwarg.
Use the colors kwarg to contourf and manually specify the colors
Use a discrete colormap constructed with matplotlib.colors.from_levels_and_colors.
The simplest way is the first option. It is also the only option that allows you to use a continuous colormap.
The reason to use the first or third options is that they will work for any type of matplotlib plot that uses a colormap (e.g. imshow, scatter, etc).
The third option constructs a discrete colormap and normalization object from specific colors. It's basically identical to the second option, but it will a) work with other types of plots than contour plots, and b) avoids having to manually specify the number of contours.
As an example of the first option (I'll use imshow here because it makes more sense than contourf for random data, but contourf would have identical usage other than the interpolation option.):
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import DivergingNorm
data = np.random.random((10,10))
data = 10 * (data - 0.8)
fig, ax = plt.subplots()
im = ax.imshow(data, norm=DivergingNorm(0), cmap=plt.cm.seismic, interpolation='none')
fig.colorbar(im)
plt.show()
As an example of the third option (notice that this gives a discrete colormap instead of a continuous colormap):
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import from_levels_and_colors
data = np.random.random((10,10))
data = 10 * (data - 0.8)
num_levels = 20
vmin, vmax = data.min(), data.max()
midpoint = 0
levels = np.linspace(vmin, vmax, num_levels)
midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)
vals = np.interp(midp, [vmin, midpoint, vmax], [0, 0.5, 1])
colors = plt.cm.seismic(vals)
cmap, norm = from_levels_and_colors(levels, colors)
fig, ax = plt.subplots()
im = ax.imshow(data, cmap=cmap, norm=norm, interpolation='none')
fig.colorbar(im)
plt.show()