Plotly is an open-source, interactive graphing library for Python - Plotly GitHub repo
- https://github.com/plotly/plotly.py
- https://plot.ly/python/
- https://plot.ly/python/reference/
- https://plot.ly/python/reference/#layout
- https://plot.ly/python/getting-started/
- https://plot.ly/python/user-guide/
- https://plot.ly/python/static-image-export/#write-image-file
- https://plot.ly/python/setting-graph-size/
- https://plot.ly/python/bar-charts/
- https://plot.ly/python/horizontal-bar-charts/
- https://plotly.com/python/plotly-express/
First install the package using Pip, if necessary:
pip install plotly
When using the Plotly package, we can use the high-level "plotly express" interface (RECOMMENDED), or the lower level "graph objects" interface (see corresponding sections below).
When using Plotly Express, we choose a charting function, like bar
(see docs), and pass in some data along with some chart customization options, like adding a title, axis labels, etc.
We can pass the data to the charting function using a pandas.DataFrame
object, or raw data (lists). See corresponding sections below.
Basic bar:
from plotly.express import bar
# FYI: these lists are related based on their order (e.g. assume Romantic Comedy has 121212 viewers)
genres = ['Romantic Comedy', 'Thriller', 'Mystery', 'Documentary', 'Action', 'Fantasy', 'Sci-Fi']
viewers = [121212, 123456, 234567, 283105, 544099, 876543, 987654]
fig = bar(x=genres, y=viewers)
fig.show()
With title and axis labels:
fig = bar(x=genres, y=viewers, title="Viewers per Genre", labels={"y": "Genre", "x": "Viewers"})
fig.show()
Horizontal bar:
# NOTE: for horizontal orientation, need to switch x and y
fig = bar(x=viewers, y=genres, orientation="h", title="Viewers per Genre", labels={"y": "Genre", "x": "Viewers"})
fig.show()
With colors:
fig = bar(x=viewers, y=genres, orientation="h", title="Viewers per Genre", labels={"y": "Genre", "x": "Viewers"},
color=viewers, color_continuous_scale="Pinkyl"
)
fig.show()
Once you learn about pandas.DataFrame
objects, we can chart them similarly, but we reference the column names of the DataFrame
:
from pandas import DataFrame
from plotly.express import bar
df = DataFrame([
{"genre": "Thriller", "viewers": 123456},
{"genre": "Mystery", "viewers": 234567},
{"genre": "Sci-Fi", "viewers": 987654},
{"genre": "Fantasy", "viewers": 876543},
{"genre": "Documentary", "viewers": 283105},
{"genre": "Action", "viewers": 544099},
{"genre": "Romantic Comedy", "viewers": 121212}
])
fig = bar(data_frame=df, x="genre", y="viewers")
fig.show()
# ... etc.
fig = bar(data_frame=df, x="genre", y="viewers"
# from the docs: "The keys of this dict should correspond to column names,
# and the values should correspond to the desired label to be displayed.
labels={"genre": "Movie Genre", "viewers": "Viewers"},
color="viewers"
)
fig.show()
For learning purposes, prefer to construct charts using the "offline" versions which don't require a Plotly account or API key.
To display a new chart, construct it by specifying certain chart configuration options, including the type of chart (e.g. scatterplot), and the data to visualize:
# adapted from: https://plot.ly/python/getting-started/#initialization-for-offline-plotting
import plotly
import plotly.graph_objs as go
plotly.offline.plot({
"data": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
"layout": go.Layout(title="hello world")
}, auto_open=True)
NOTE: after a few seconds, the chart will automatically open in your web browser.
Consult the documentation and examples for a variety of chart customization options.
A pie chart example:
# adapted from: https://plot.ly/python/pie-charts/
import plotly
import plotly.graph_objs as go
labels = ["Oxygen", "Hydrogen", "Carbon_Dioxide", "Nitrogen"]
values = [4500, 2500, 1053, 500]
trace = go.Pie(labels=labels, values=values)
plotly.offline.plot([trace], filename="basic_pie_chart.html", auto_open=True)
# assuming df is some DataFrame with columns of "timestamp", "closing_price", and "trendline"...
from plotly.graph_objects import Figure, Scatter
closing_prices = Scatter(x=df["timestamp"], y=df["closing_price"], name='Closing Prices')
trendline = Scatter(x=df["timestamp"], y=df["trendline"], name='50 day Moving Average')
fig = Figure(data=[closing_prices, trendline])
fig.update_layout(title="My Chart Title")
fig.show()