Plotly’s Python graphing library makes interactive, publication-quality graphs. Examples of how to make line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, polar charts, and bubble charts.

Code
import plotly
print(plotly.__version__)
Code
from plotnine import *
import seaborn as sns
import plotly.express as px
import pandas as pd

# Apply the default theme


# Load an example dataset
tips = sns.load_dataset("tips")
tips.head()

1 Scatter Plot

Code
fig = px.scatter(tips,x="tip", y="total_bill")
fig.show()

1.1 color by group

Code
fig = px.scatter(tips,x="tip", y="total_bill",color="sex")
fig.show()

1.2 size by group

Code
fig = px.scatter(tips,x="tip", y="total_bill",size="size")
fig.show()

2 line Plot

Code
dowjones= sns.load_dataset("dowjones")
dowjones.head()
Code
import plotly.express as px
fig = px.line(dowjones, x="Date", y="Price")
fig.show()

2.1 color by group

Code
import random
from siuba import _, mutate, filter, group_by, summarize,show_query
from siuba import *

dowjones2=dowjones>>mutate(type='old')

dowjones3=dowjones>>mutate(Price=_.Price+random.random()*200,type='new')

dowjones4=pd.concat([dowjones2, dowjones3], ignore_index = True)>> arrange(_.Date)

df = px.data.gapminder().query("continent=='Oceania'")
Code
dowjones4.head()
Code
import plotly.express as px

fig = px.line(dowjones4, x="Date", y="Price", color='type')
fig.show()

3 histogram

Code
fig = px.histogram(tips, x="total_bill")
fig.show()

3.1 color by group

Code
fig = px.histogram(tips, x="total_bill", color='sex', barmode='group')
fig.show()

4 bar chart

Code
fig = px.bar(tips, x='sex', y='total_bill',color='sex')
fig.show()

5 box plot

Code
fig = px.box(tips, y="total_bill")
fig.show()

5.1 color by group

Code
fig = px.box(tips, y="total_bill",x='sex',color='sex')
fig.show()

6 strip plot

Code
fig = px.strip(tips,x="day", y="total_bill")
fig.show()

6.1 color by group

Code
fig = px.strip(tips,x="day", y="total_bill",color='sex')
fig.show()

7 Facet plot

Code
fig = px.scatter(tips, x="total_bill", y="tip", color="sex", facet_col="day")
fig.show()

make 3 plot per row

Code
fig = px.scatter(tips, x="total_bill", y="tip", color="sex", facet_col="day",facet_col_wrap=3)
fig.show()

8 title,size, x y names

8.1 add title

Code
fig = px.scatter(tips,x="tip", y="total_bill", title="total_bill title").update_layout(title_x=0.5)
fig.show()

8.2 adjust size

Code
fig = px.scatter(tips,x="tip", y="total_bill")

fig.update_layout(
    autosize=False
    ,width=200
    ,height=200
    )
    
fig.show()

8.3 change x y name

Code
fig = px.scatter(tips,x="tip", y="total_bill"
                ,labels={
                     "tip": "new x label)",
                     "total_bill": "new y label"
                 }

)

    
fig.show()

9 applying themes:

10 Save plot

install package with pip install -U kaleido

Code
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
fig.write_image("yourfile.png") 

11 Animation plot only

https://plotly.com/python/animations/

Although Plotly Express supports animation for many chart and map types, smooth inter-frame transitions are today only possible for scatter and bar

Code
import plotly.express as px
df = px.data.gapminder()
px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])

12 reference:

https://plotly.com/python/

Back to top