Skip to content

Latest commit

 

History

History
208 lines (121 loc) · 5.92 KB

plotly.md

File metadata and controls

208 lines (121 loc) · 5.92 KB

The plotly Package

Plotly is an open-source, interactive graphing library for Python - Plotly GitHub repo

Reference

Installation

First install the package using Pip, if necessary:

pip install plotly

Usage

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).

Plotly Express

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.

Plotly Express with Raw Data

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()

newplot (2)

With title and axis labels:

fig = bar(x=genres, y=viewers, title="Viewers per Genre", labels={"y": "Genre", "x": "Viewers"})
fig.show()

newplot (3)

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()

newplot (4)

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()

newplot (5)

Plotly Express with Data Frames

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()

Graph Objects

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)

the resulting chart - a boring straight line with four points

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.

More Examples

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)

the resulting chart - a pie chart with four slices

Charting Multiple Graph Objects

# 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()

Screen Shot 2022-02-16 at 2 58 18 PM