Show Sidebar Hide Sidebar

Log Plots in Pandas

How to make a plot with logarithmic axes in pandas.

# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import pandas as pd

df = pd.read_csv('http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt', sep='\t')
df2007 = df[df.year==2007]
df1952 = df[df.year==1952]
df.head(2)

fig = {
    'data': [
        {
            'x': df[df['year']==year]['gdpPercap'],
            'y': df[df['year']==year]['lifeExp'],
            'name': year, 'mode': 'markers',
        } for year in [1952, 1982, 2007]
    ],
    'layout': {
        'xaxis': {'title': 'GDP per Capita', 'type': 'log'},
        'yaxis': {'title': "Life Expectancy"}
    }
}

# IPython notebook
# py.iplot(fig, filename='pandas/grouped-scatter')

url = py.plot(fig, filename='pandas/grouped-scatter')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import cufflinks as cf
import pandas as pd

cf.set_config_file(world_readable=True, offline=False)

df = pd.DataFrame([x**2] for x in range(100))

# iplot now supports logarithmic charts for boths Y and X axis, 
# by using logy = True and logx = True respectively
df.iplot(logy=True)
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.