Search code examples
python-2.7matplotlibplotscale

Plot lines in log-scale (Python)


I'm trying to plot a dash-line in a plot using:

ax.plot([dt.datetime(2012,01,27,18,19),
         dt.datetime(2012,01,27,18,19)], [0, 1300], 'k--', lw=2)

It works fine when I use linnear scale but when I define a log scale

ax.set_yscale('log')

the line dont appear


Solution

  • It won't show because you have the number 0 on the y axis, change it to something positive, e.g. 1:

    import matplotlib.pyplot as plt
    import datetime as dt
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot([dt.datetime(2012,01,27,18,19),
         dt.datetime(2012,01,27,18,19)], [1, 1300], 'k--', lw=2)
    ax.set_yscale('log')
    plt.show()
    

    Explanation:

    log(0) is not defined. Numpy returns -inf (which has some logic behind it), but if you try to draw a point with inf or nan value, it is not going to be drawn. With plots made of line segments this means that two line segments are going to disappear. Now you tried to draw a line between an existing point and a non-existing point. (You can verify this by changing the style to 'o'.)