Show Sidebar Hide Sidebar

Line Charts in Pandas

How to make line charts 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 plotly.graph_objs as go

import pandas as pd
import numpy as np

N = 500
x = np.linspace(0, 1, N)
y = np.random.randn(N)
df = pd.DataFrame({'x': x, 'y': y})
df.head()

data = [
    go.Scatter(
        x=df['x'], # assign x as the dataframe column 'x'
        y=df['y']
    )
]
# IPython notebook
# py.iplot(data, filename='pandas/basic-line-plot')

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

N = 500
x = np.linspace(0, 1, N)
y = np.random.randn(N)
df = pd.DataFrame({'x': x, 'y': y})
df.head()

import plotly.plotly as py
import plotly.graph_objs as go

data = [
    go.Scatter(
        x=df['x'], # assign x as the dataframe column 'x'
        y=df['y']
    )
]

layout = go.Layout(
    title='scatter plot with pandas',
    yaxis=dict(title='random distribution'),
    xaxis=dict(title='linspace')
)

fig = go.Figure(data=data, layout=layout)

# IPython notebook
# py.iplot(fig, filename='pandas/line-plot-title')

url = py.plot(fig, filename='pandas/line-plot-title')
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd
import numpy as np

x = np.linspace(0, 1, N)
y = np.random.randn(N)
df = pd.DataFrame({'x': x, 'y': y})
df.head()

x2 = np.linspace(0, 1, N)
y2 = np.random.randn(N) + 3
df2 = pd.DataFrame({'x': x2, 'y': y2})
df2.head()

data = [
    go.Scatter(
        x=df['x'], # assign x as the dataframe column 'x'
        y=df['y'],
        name='random around 0'
    ),
    go.Scatter(
        x=df2['x'], # assign x as the dataframe column 'x'
        y=df2['y'],
        name='random around 3'
    )
]

url = py.plot(data, filename='pandas-line-naming-traces')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api
# Cufflinks binds plotly to pandas dataframes in IPython notebook. Read more

import plotly.plotly as py
import cufflinks as cf
import pandas as pd

cf.set_config_file(offline=False, world_readable=True, theme='ggplot')

df = cf.datagen.lines()
df.head()

df.iplot(kind='scatter', filename='cufflinks/cf-simple-line')

# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api
# Cufflinks binds plotly to pandas dataframes in IPython notebook. Read more

import plotly.plotly as py
import cufflinks as cf
import pandas as pd

cf.set_config_file(offline=False, world_readable=True, theme='ggplot')


# Create a simple dataframe..
df = cf.datagen.lines(3)

colors = ['red', 'blue', 'black'] # Individual Line Color
dashes = ['solid', 'dash', 'dashdot'] # Individual Line Style
widths = [2, 4, 6] # Individual Line Width

plot_url = df.iplot(kind='scatter', mode='lines', colors=colors, dash=dashes, filename='cufflinks/line-style-and-color')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api
# Cufflinks binds plotly to pandas dataframes in IPython notebook. Read more

import plotly.plotly as py
import cufflinks as cf
import pandas as pd

cf.set_config_file(offline=False, world_readable=True, theme='ggplot')

df = pd.DataFrame(np.random.randn(1000, 2), columns=['A', 'B']).cumsum()
df.iplot(filename='cufflinks/line-example')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api
# Cufflinks binds plotly to pandas dataframes in IPython notebook. Read more

import plotly.plotly as py
import cufflinks as cf
import pandas as pd

df=cf.datagen.lines(3,columns=['a','b','c'])

figure = df.iplot(kind='scatter', asFigure=True)
print figure.to_string()

figure['layout']['yaxis1'].update({'title': 'Price', 'tickprefix': '$'})
for i, trace in enumerate(figure['data']):
    trace['name'] = 'Trace {}'.format(i)
    
py.iplot(figure, filename='cufflinks/customized-chart')


Still need help?
Contact Us

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