CSV or comma-delimited-values is a very popular format for storing structured data. In this tutorial, we will see how to plot beautiful graphs using csv data, and Pandas.
First, let's import the data and look at it.
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv')
df.head()
This code reads a CSV file from a URL and displays the first few rows of the dataframe.
Plotting with Pandas
Next, let's plot the data using Pandas.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv')
df.plot() # plots all columns against index
df.plot(kind='scatter', x='AAPL_x', y='AAPL_y') # scatter plot
df.plot(kind='density') # estimate density function
The df.plot()
method is a wrapper around Matplotlib's plot, and allows us to create various types of plots. We can also specify the type of plot using the kind
parameter.
Plotting with Plotly Express
Now, let's use Plotly Express to plot the data.
import pandas as pd
import plotly.express as px
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv')
fig = px.line(df, x='AAPL_x', y='AAPL_y', title='Apple Share Prices over time (2014)')
fig.show()
Plotly Express is a higher-level interface for creating plots. We can create various types of plots using this library.
Plotting with graph_objects
Finally, let's use the graph_objects
module to plot the data.
import pandas as pd
import plotly.graph_objects as go
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv')
fig = go.Figure(go.Scatter(x=df['AAPL_x'], y=df['AAPL_y'],
name='Share Prices (in USD)'))
fig.update_layout(title='Apple Share Prices over time (2014)',
plot_bgcolor='rgb(230, 230,230)',
showlegend=True)
fig.show()
The graph_objects
module provides a lower-level interface for creating plots. We can create various types of plots using this library.
What About Dash?
Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. You can use Dash to build interactive dashboards that display your plots.
For example:
import plotly.graph_objects as go
from dash import Dash, dcc, html
app = Dash()
app.layout = html.Div([
dcc.Graph(figure=fig)
])
app.run_server(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
This code creates a simple dashboard that displays the plot.
I hope this tutorial has been helpful in showing you how to plot CSV data in Python using Pandas and Plotly.