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. We will learn how to import csv data from an external source (a url), and plot it using Plotly and pandas.
First we 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()
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()
Plot from CSV in Dash¶
Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run
pip install dash, click "Download" to get the code and run
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()
What About Dash?¶
Learn about how to install Dash at https://dash.plot.ly/installation.
Everywhere in this page that you see
fig.show(), you can display the same figure in a Dash application by passing it to the
figure argument of the
Graph component from the built-in
dash_core_components package like this:
import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) import dash import dash_core_components as dcc import dash_html_components as html app = dash.Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run_server(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter