|
| 1 | +### Source |
| 2 | +https://plotly.com/python/plotly-express/ |
| 3 | +### Libraries |
| 4 | +import plotly.express as px |
| 5 | +import plotly.io as pio #pio.templates |
| 6 | +import plotly.graph_objects as go |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | +import numpy as np |
| 10 | +### Scatter plots |
| 11 | +df = px.data.iris() |
| 12 | +fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species") |
| 13 | +fig.show() |
| 14 | +### Trendline |
| 15 | +#Disable default theming |
| 16 | +#pio.templates.default = "none" |
| 17 | +pio.templates.default = "plotly_dark" |
| 18 | + |
| 19 | +df = px.data.iris() |
| 20 | +fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", marginal_y="violin", |
| 21 | + marginal_x="box", trendline="ols", template="simple_white") |
| 22 | +fig.show() |
| 23 | +### Error bars |
| 24 | +df = px.data.iris() |
| 25 | +df["e"] = df["sepal_width"]/100 |
| 26 | +fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", error_x="e", error_y="e") |
| 27 | +fig.show() |
| 28 | +### Bar charts |
| 29 | +df = px.data.tips() |
| 30 | +fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group") |
| 31 | +fig.show() |
| 32 | +### Facet plots |
| 33 | +df = px.data.tips() |
| 34 | +fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group", facet_row="time", facet_col="day", |
| 35 | + category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]}) |
| 36 | +fig.show() |
| 37 | +### Scatterplot matrices |
| 38 | +df = px.data.iris() |
| 39 | +fig = px.scatter_matrix(df, dimensions=["sepal_width", "sepal_length", "petal_width", "petal_length"], color="species") |
| 40 | +fig.show() |
| 41 | +### Parallel Coordinates |
| 42 | +df = px.data.iris() |
| 43 | +fig = px.parallel_coordinates(df, color="species_id", labels={"species_id": "Species", |
| 44 | + "sepal_width": "Sepal Width", "sepal_length": "Sepal Length", |
| 45 | + "petal_width": "Petal Width", "petal_length": "Petal Length", }, |
| 46 | + color_continuous_scale=px.colors.diverging.Tealrose, color_continuous_midpoint=2) |
| 47 | +fig.show() |
| 48 | +### Parallel Categories |
| 49 | +df = px.data.tips() |
| 50 | +fig = px.parallel_categories(df, dimensions=['sex', 'smoker', 'day'], |
| 51 | + color="size", color_continuous_scale=px.colors.sequential.Inferno, |
| 52 | + labels={'sex':'Payer sex', 'smoker':'Smokers at the table', 'day':'Day of week'}) |
| 53 | +fig.show() |
| 54 | +### Continuous Color |
| 55 | +df = px.data.tips() |
| 56 | +fig = px.scatter(df, x="total_bill", y="tip", color="size", |
| 57 | + title="Numeric 'size' values mean continous color") |
| 58 | + |
| 59 | +fig.show() |
| 60 | +### Animations |
| 61 | +df = px.data.gapminder() |
| 62 | +fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country", |
| 63 | + size="pop", color="continent", hover_name="country", facet_col="continent", |
| 64 | + log_x=True, size_max=45, range_x=[100,100000], range_y=[25,90]) |
| 65 | +fig.show() |
| 66 | +### line charts |
| 67 | +df = px.data.gapminder() |
| 68 | +fig = px.line(df, x="year", y="lifeExp", color="continent", line_group="country", hover_name="country", |
| 69 | + line_shape="spline", render_mode="svg") |
| 70 | +fig.show() |
| 71 | +### Area charts |
| 72 | +df = px.data.gapminder() |
| 73 | +fig = px.area(df, x="year", y="pop", color="continent", line_group="country") |
| 74 | +fig.show() |
| 75 | +### Pie charts |
| 76 | +df = px.data.gapminder().query("year == 2007").query("continent == 'Europe'") |
| 77 | +df.loc[df['pop'] < 2.e6, 'country'] = 'Other countries' # Represent only large countries |
| 78 | +fig = px.pie(df, values='pop', names='country', title='Population of European continent') |
| 79 | +fig.show() |
| 80 | +### Sunburst charts |
| 81 | +df = px.data.gapminder().query("year == 2007") |
| 82 | +fig = px.sunburst(df, path=['continent', 'country'], values='pop', |
| 83 | + color='lifeExp', hover_data=['iso_alpha']) |
| 84 | +fig.show() |
| 85 | +### Treemaps |
| 86 | +df = px.data.gapminder().query("year == 2007") |
| 87 | +fig = px.treemap(df, path=[px.Constant('world'), 'continent', 'country'], values='pop', |
| 88 | + color='lifeExp', hover_data=['iso_alpha']) |
| 89 | +fig.show() |
| 90 | +### Histograms |
| 91 | +df = px.data.tips() |
| 92 | +fig = px.histogram(df, x="total_bill", y="tip", color="sex", marginal="rug", hover_data=df.columns) |
| 93 | +fig.show() |
| 94 | +### Box plots |
| 95 | +df = px.data.tips() |
| 96 | +fig = px.box(df, x="day", y="total_bill", color="smoker", notched=True) |
| 97 | +fig.show() |
| 98 | + |
| 99 | +# Explanation https://www.statisticshowto.com/probability-and-statistics/descriptive-statistics/box-plot/ |
| 100 | +### Density contours |
| 101 | +df = px.data.iris() |
| 102 | +fig = px.density_contour(df, x="sepal_width", y="sepal_length", color="species", marginal_x="rug", marginal_y="histogram") |
| 103 | +fig.show() |
| 104 | +### Density Heatmaps |
| 105 | +df = px.data.iris() |
| 106 | +fig = px.density_heatmap(df, x="sepal_width", y="sepal_length", marginal_x="rug", marginal_y="histogram") |
| 107 | +fig.show() |
| 108 | +### Choropleth maps |
| 109 | +df = px.data.gapminder() |
| 110 | +fig = px.choropleth(df, locations="iso_alpha", color="lifeExp", hover_name="country", animation_frame="year", range_color=[20,80]) |
| 111 | +fig.show() |
| 112 | +### 3D scatter plots |
| 113 | +df = px.data.election() |
| 114 | +fig = px.scatter_3d(df, x="Joly", y="Coderre", z="Bergeron", color="winner", size="total", hover_name="district", |
| 115 | + symbol="result", color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"}) |
| 116 | +fig.show() |
| 117 | +### Ternary charts |
| 118 | +df = px.data.election() |
| 119 | +fig = px.scatter_ternary(df, a="Joly", b="Coderre", c="Bergeron", color="winner", size="total", hover_name="district", |
| 120 | + size_max=15, color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"} ) |
| 121 | +fig.show() |
0 commit comments