Code
import plotly
print(plotly.__version__)
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.
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'")
make 3 plot per row
install package with pip install -U kaleido
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
https://plotly.com/python/
---
title: "Plotly chart"
execute:
warning: false
error: false
eval: false
format:
html:
toc: true
toc-location: right
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
---
{width="650"}
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.
```{python}
import plotly
print(plotly.__version__)
```
```{python}
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()
```
# Scatter Plot
```{python}
fig = px.scatter(tips,x="tip", y="total_bill")
fig.show()
```
## color by group
```{python}
fig = px.scatter(tips,x="tip", y="total_bill",color="sex")
fig.show()
```
## size by group
```{python}
fig = px.scatter(tips,x="tip", y="total_bill",size="size")
fig.show()
```
# line Plot
```{python}
dowjones= sns.load_dataset("dowjones")
dowjones.head()
```
```{python}
import plotly.express as px
fig = px.line(dowjones, x="Date", y="Price")
fig.show()
```
## color by group
```{python}
#| code-fold: true
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'")
```
```{python}
dowjones4.head()
```
```{python}
import plotly.express as px
fig = px.line(dowjones4, x="Date", y="Price", color='type')
fig.show()
```
# histogram
```{python}
fig = px.histogram(tips, x="total_bill")
fig.show()
```
## color by group
```{python}
fig = px.histogram(tips, x="total_bill", color='sex', barmode='group')
fig.show()
```
# bar chart
```{python}
fig = px.bar(tips, x='sex', y='total_bill',color='sex')
fig.show()
```
# box plot
```{python}
fig = px.box(tips, y="total_bill")
fig.show()
```
## color by group
```{python}
fig = px.box(tips, y="total_bill",x='sex',color='sex')
fig.show()
```
# strip plot
```{python}
fig = px.strip(tips,x="day", y="total_bill")
fig.show()
```
## color by group
```{python}
fig = px.strip(tips,x="day", y="total_bill",color='sex')
fig.show()
```
# Facet plot
```{python}
fig = px.scatter(tips, x="total_bill", y="tip", color="sex", facet_col="day")
fig.show()
```
make 3 plot per row
```{python}
fig = px.scatter(tips, x="total_bill", y="tip", color="sex", facet_col="day",facet_col_wrap=3)
fig.show()
```
# title,size, x y names
## add title
```{python}
fig = px.scatter(tips,x="tip", y="total_bill", title="total_bill title").update_layout(title_x=0.5)
fig.show()
```
## adjust size
```{python}
fig = px.scatter(tips,x="tip", y="total_bill")
fig.update_layout(
autosize=False
,width=200
,height=200
)
fig.show()
```
## change x y name
```{python}
fig = px.scatter(tips,x="tip", y="total_bill"
,labels={
"tip": "new x label)",
"total_bill": "new y label"
}
)
fig.show()
```
# applying themes:
::: {.panel-tabset .nav-pills}
## ggplot2 themes
```{python}
fig = px.scatter(tips,x="tip", y="total_bill",template="ggplot2")
fig.show()
```
## seaborn themes
```{python}
fig = px.scatter(tips,x="tip", y="total_bill",template="seaborn")
fig.show()
```
## plotly_dark themes
```{python}
fig = px.scatter(tips,x="tip", y="total_bill",template="plotly_dark")
fig.show()
```
:::
# Save plot
install package with pip install -U kaleido
```{python}
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
fig.write_image("yourfile.png")
```
# 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
```{python}
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])
```
# reference:
https://plotly.com/python/