Search code examples
pythonmatplotliblogarithm

Range of logarithmic axis of scatter plot is not being set appropriately by matplotlib.pyplot


I'm trying to plot 5 points (x, y) on a 2D scatter plot with a logarithmic y axis. The plot is produced but the range of the y-axis is not chosen well so only one point is displayed. The problem goes away when I remove the first point (0.38, 0.005).

Is this a bug in matplotlib?

Could someone try reproducing this?

import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
emissions_abs_pts = np.array([
    [0.38, 0.005], # without this point it scales appropriately
    [0.42, 0.05],
    [0.67, 0.5],
    [0.96, 5.0],
    [1.0, 50.0]
])
fig, ax = plt.subplots(1, 1)
ax.scatter(emissions_abs_pts[:,0], emissions_abs_pts[:,1])
ax.set_yscale('log')

Here is what the plot looks like with all five points included: enter image description here

Note the y-axis range is 10^1 to 10^2

Here is the plot with the first point commented-out: enter image description here

I'm using %matplotlib inline with a jupyter notebook running Python 2.7.


Solution

  • edit: I got the same scaling error.

    Add this to the end of your code

    ax.set_ylim([0.001, 100])
    

    It will force the axis to behave.

    It might be a jupyter thing?
    When I ran your code as a script from the command line (altering it not to be "inline"), the axes scaled correctly without the need to force them.