Matplotlib logarithmic scatter plot

Discussion in 'Python' started by Derek Basch, Feb 27, 2006.

  1. Derek Basch

    Derek Basch Guest

    Can anyone give any suggestions on how to make a logarithmic (base 10)
    x and y axis (loglog) plot in matplotlib? The scatter function doesn't
    seem to have any log functionality built into it.

    Thanks,
    Derek Basch

    P.S. I suck at math so feel free to make me feel stupid if it is really
    easy to do :).
    Derek Basch, Feb 27, 2006
    #1
    1. Advertising

  2. Derek Basch

    Bas Guest

    Try this, don't know if this works for al versions:

    from pylab import *
    x=10**linspace(0,5,100)
    y=1/(1+x/1000)
    loglog(x,y)
    show()

    If you only need a logarithm along one axis you can use semilogx() or
    semilogy(). For more detailed questions go to the matplotlib mailing
    list.

    Cheers,
    Bas
    Bas, Feb 27, 2006
    #2
    1. Advertising

  3. Derek Basch

    Derek Basch Guest

    Thanks for the reply. I need a scatter plot though. Can that be done?
    Derek Basch, Feb 27, 2006
    #3
  4. Derek Basch

    John Hunter Guest

    >>>>> "Derek" == Derek Basch <> writes:

    Derek> Thanks for the reply. I need a scatter plot though. Can
    Derek> that be done?

    You can set the scale of xaxis and yaxis to either log or linear for
    scatter plots

    In [33]: ax = subplot(111)

    In [34]: ax.scatter( 1e6*rand(1000), rand(1000))
    Out[34]: <matplotlib.collections.RegularPolyCollection instance at
    0x9c32fac>

    In [35]: ax.set_xscale('log')

    In [36]: ax.set_xlim(1e-6,1e6)
    Out[36]: (9.9999999999999995e-07, 1000000.0)

    In [37]: draw()
    John Hunter, Feb 27, 2006
    #4
  5. Derek Basch

    Derek Basch Guest

    Great! That worked fine after I played with it for a bit. One last
    question though. How do I label the ticks with the product of the
    exponentiation? For instance:

    100

    instead of

    10**2

    Thanks for all the help,
    Derek Basch
    Derek Basch, Feb 28, 2006
    #5
  6. Derek Basch

    John Hunter Guest

    >>>>> "Derek" == Derek Basch <> writes:

    Derek> Great! That worked fine after I played with it for a
    Derek> bit. One last question though. How do I label the ticks
    Derek> with the product of the exponentiation? For instance:

    Derek> 100

    Derek> instead of

    Derek> 10**2

    You can supply your own custom tick formatters (and locators). See

    http://matplotlib.sf.net/matplotlib.ticker.html

    and examples

    http://matplotlib.sourceforge.net/examples/custom_ticker1.py
    http://matplotlib.sourceforge.net/examples/major_minor_demo1.py
    http://matplotlib.sourceforge.net/examples/major_minor_demo2.py

    JDH
    John Hunter, Feb 28, 2006
    #6
  7. Derek Basch

    Derek Basch Guest

    Thanks again. Here is the finished product. Maybe it will help someone
    in the future:

    from pylab import *

    def log_10_product(x, pos):
    """The two args are the value and tick position.
    Label ticks with the product of the exponentiation"""
    return '%1i' % (x)

    ax = subplot(111)
    # Axis scale must be set prior to declaring the Formatter
    # If it is not the Formatter will use the default log labels for ticks.
    ax.set_xscale('log')
    ax.set_yscale('log')

    formatter = FuncFormatter(log_10_product)
    ax.xaxis.set_major_formatter(formatter)
    ax.yaxis.set_major_formatter(formatter)

    ax.scatter( [3, 5, 70, 700, 900], [4, 8, 120, 160, 200], s=8, c='b',
    marker='s', faceted=False)
    ax.scatter( [1000, 2000, 3000, 4000, 5000], [2000, 4000, 6000, 8000,
    1000], s=8, c='r', marker='s', faceted=False)

    ax.set_xlim(1e-1, 1e5)
    ax.set_ylim(1e-1, 1e5)
    grid(True)
    xlabel(r"Result", fontsize = 12)
    ylabel(r"Prediction", fontsize = 12)

    show()
    Derek Basch, Feb 28, 2006
    #7
  8. Derek Basch

    John Hunter Guest

    >>>>> "Derek" == Derek Basch <> writes:

    Derek> formatter = FuncFormatter(log_10_product)
    Derek> ax.xaxis.set_major_formatter(formatter)
    Derek> ax.yaxis.set_major_formatter(formatter)

    I would caution you against using identical objects for the x and y
    axis *Locators*. For the formatters, it will do no harm, but for the
    locators you can get screwed up because the locator object reads the
    axis data and view limits when making it's choices.

    Ie, do not do this:

    ax.xaxis.set_major_locator(locator)
    ax.yaxis.set_major_locator(locator)

    but rather do this

    ax.xaxis.set_major_locator(MyLocator())
    ax.yaxis.set_major_locator(Mylocator())

    Thanks for the example,
    JDH
    John Hunter, Mar 1, 2006
    #8
  9. Derek Basch

    Derek Basch Guest

    Good tip John. Hopefully it will help someone out. Thanks again.
    Derek Basch
    Derek Basch, Mar 1, 2006
    #9
  10. Derek Basch

    evander21

    Joined:
    Nov 3, 2010
    Messages:
    1
    You can also keep plot() from connecting the points

    In the process of trying to solve what I think is the same problem I ran across a potential more convenient and elegant solution. The following command effectively creates a loglog scatter plot:

    import matplotlib.pyplot as plt

    plt.loglog(x,y,marker='o',linestyle='none')

    hope this helps,

    evan
    evander21, Nov 3, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dr. Colombes
    Replies:
    1
    Views:
    1,031
    John Hunter
    Feb 10, 2005
  2. C Barrington-Leigh
    Replies:
    1
    Views:
    634
    C Barrington-Leigh
    Sep 12, 2010
  3. Johannes Korn

    extensive scatter plot

    Johannes Korn, Nov 24, 2010, in forum: Python
    Replies:
    1
    Views:
    471
    Robert Kern
    Nov 24, 2010
  4. Replies:
    1
    Views:
    103
    EricGarlic
    Nov 26, 2013
  5. Jamie Mitchell

    Adding R squared value to scatter plot

    Jamie Mitchell, May 21, 2014, in forum: Python
    Replies:
    2
    Views:
    68
    Jamie Mitchell
    Jun 5, 2014
Loading...

Share This Page