diff --git a/documentation/02.2-eda-plotly.md b/documentation/02.2-eda-plotly.md index 0a65161..d8114aa 100644 --- a/documentation/02.2-eda-plotly.md +++ b/documentation/02.2-eda-plotly.md @@ -542,52 +542,280 @@ aux éléments du dessin. ```python {.numberLines} import pandas as pd from plotly import express as px +from plotly.colors import qualitative data = pd.DataFrame(data={ - "product": ["pomme", "poire", "banane", "pêche"], - "price": [1.99, 2.49, 2.99, 3.49], "wpu": [200, 180, 140, 200] + "name": ["headphones", "microphone", "speaker", "powerbank", "tablet", "laptop"], + "price": [99.99, 49.99, 149.99, 29.99, 199.99, 499.99], + "merchant": ["amazon", "amazon", "bestbuy", "amazon", "newegg", "amazon"], + "color": ["black", "silver", "white", "red", "gray", "silver"] }) -figure = px.pie(data, values="price", names="product", title="Prix", color_discrete_sequence=["red", "orange", "yellow", "#8F0"]) -figure.layout.update({"template": "seaborn", "title": "Prix au kilo", "font": {"family": "Cabin", "size": 13}}) -figure.show() +figure = px.pie(data, values="price", names="name", title="Prix", texttemplate="", color_discrete_sequence=qualitative.Bold) +figure.layout.update({"template": "seaborn", "title": "Prix à l'unité", "font": {"family": "Cabin", "size": 13}}) +figure.show(renderer="browser") ``` -Plotly Express fournit des séquences ou dégradés de couleurs, dont la référence est disponible ci-dessous : +---- -[Couleurs et séquences de couleurs](https://plotly.com/python/discrete-color/) +![Rendu avec choix de couleurs par secteur](assets/images/eda-plotly-express-pie-colors-predefined.png) ---- -![Rendu avec choix de couleurs par secteur](assets/images/eda-plotly-express-pie-colors.png) +### Gradients de couleurs ----- - -### Couleur dépendante de la valeur - -Vous pouvez sur certains graphiques définir un gradient à appliquer aux éléments du dessin -selon une valeur qui leur est associée. Ici, nous avons un graphique en barres, où la couleur -de chaque barre dépend de la valeur de la colonne `price`. +Vous pouvez appliquer des couleurs prédéfinies ou continues à vos graphiques. Vous pouvez utiliser une palette +servant de dégradé pour colorier les éléments du dessin selon une valeur associée. ```python {.numberLines} import pandas as pd from plotly import express as px +from plotly.colors import sequential, qualitative -data = pd.DataFrame(data={ - "product": ["pomme", "poire", "banane", "pêche"], - "price": [1.99, 2.49, 2.99, 3.49], "wpu": [200, 180, 140, 200] + +df = pd.DataFrame(data={ + "month": [f"2005-{i:02d}" for i in range(1, 13)], + "temperature": [4.4, 6.8, 9.2, 11.5, 13.8, 16.3, 19.1, 21.4, 22.9, 20.5, 17.2, 13.9] }) -figure = px.bar(data, y="price", x="product", title="Prix", color="price", color_continuous_scale=["red", "orange", "yellow", "#8F0"]) -figure.layout.update({"template": "seaborn", "title": "Prix au kilo", "font": {"family": "Cabin", "size": 13}}) -figure.show() +plot = px.bar( + df, x="month", y="temperature", color="temperature", template="seaborn", + title="Temperatures mensuelles de 2005", + color_continuous_scale=sequential.Turbo, + labels={"month": "Mois", "temperature": "Température (°C)"} +) +plot.layout.update({"font": {"family": "Cabin", "size": 13}}) +plot.show() ``` -Ici, l'argument `color` permet d'indiquer sur les valeurs de quelle colonne colorier les barres. -L'argument `color_continuous_scale` permet de définir les couleurs d'un dégradé à utiliser pour -colorier les barres. - ---- -![Rendu avec gradient de couleurs](assets/images/eda-plotly-express-bar-gradient.png) +![Rendu avec choix de couleurs par valeur](assets/images/eda-plotly-express-bar-colors-predefined.png) + +---- + +### Palettes de couleurs personnalisées + +Les palettes utilisées dans les exemples précédents sont simplement des listes de couleurs prédéfinies +par Plotly, mais vous pouvez définir vos propres palettes de couleurs via des listes composées de couleurs sous plusieurs formats : + +| Format de couleur | Exemple(s) | Description / notes | +| ------------------------ | -------------------------------- | ------------------------------------------------ | +| Nom de couleur CSS | `"red"`, `"blue"`, `"green"` | Noms de couleurs standards en CSS | +| Code hexadécimal | `"#FF5733"`, `"#33FF57"` | Codes hexadécimaux RGB | +| Code RGB | `"rgb(255,0,0)"`, `"rgb(0,255,0)"` | Codes RGB avec valeurs décimales (0-255) | +| Code RGBA | `"rgba(255,0,0,0.5)"` | Codes RGBA avec valeurs décimales (0-255) et alpha (0-1) | + +---- + +Ici on définit manuellement une suite de couleurs à utiliser pour colorier les barres, allant du rouge au jaune. + +```python {.numberLines} +import pandas as pd +from plotly import express as px +from plotly.colors import sequential, qualitative + + +df = pd.DataFrame(data={ + "month": [f"2005-{i:02d}" for i in range(1, 13)], + "temperature": [4.4, 6.8, 9.2, 11.5, 13.8, 16.3, 19.1, 21.4, 22.9, 20.5, 17.2, 13.9] +}) +plot = px.bar( + df, x="month", y="temperature", color="temperature", template="seaborn", + title="Temperatures mensuelles de 2005", + color_continuous_scale=["#FF0020", "rgb(255,165,0)", "rgba(255,255,0,0.8)"], + labels={"month": "Mois", "temperature": "Température (°C)"} +) +plot.layout.update({"font": {"family": "Cabin", "size": 13}}) +plot.show() +``` + +---- + +![Rendu avec choix de couleurs par valeur](assets/images/eda-plotly-express-bar-colors-custom.png) + +---- + +[Couleurs et séquences de couleurs](https://plotly.com/python/discrete-color/) + +![Couleurs Qualitatives](assets/images/eda-plotly-colors-qualitative.svg) + +---- + +[Couleurs et séquences de couleurs](https://plotly.com/python/discrete-color/) + +![Séquences de Couleurs](assets/images/eda-plotly-colors-sequential.svg) + +---- + +[Couleurs continues](https://plotly.com/python/builtin-colorscales/#builtin-sequential-color-scales) + +![Couleurs continues](assets/images/eda-plotly-colors-continuous.svg) + +---- + +### Sous-graphiques ([subplots]{.naming}) + +Plotly propose un module `plotly.subplots` permettant de créer des figures composées de plusieurs sous-graphiques. +Vous pouvez définir la mise en page de vos sous-graphiques, en précisant le nombre de lignes et de colonnes, ainsi que +la taille de chaque sous-graphe. + +```python {.numberLines} +import pandas as pd +from plotly.subplots import make_subplots +from plotly.graph_objs import Figure, Bar + +data = pd.DataFrame(data={ + "product": ["tarte", "gâteau", "biscuit", "mille-feuille", "éclair", "brownie"], + "price": [2.99, 3.49, 1.99, 4.99, 5.99, 6.99], + "weight": [250, 300, 200, 400, 500, 600] +}) +figure: Figure = make_subplots(rows=1, cols=2, subplot_titles=("Prix", "Poids unitaires")) +figure.add_trace(Bar(name="Prix", x=data["product"], y=data["price"]), row=1, col=1) +figure.add_trace(Bar(name="Poids", x=data["product"], y=data["weight"]), row=1, col=2) +figure.update_layout(template="seaborn", title="Prix et poids unitaires", font={"family": "Cabin", "size": 13}) +figure.show(renderer="browser") +``` + +Vous pouvez également créer un objet `Figure`{.python} normalement, et exécuter la méthode `figure.set_subplots`{.python} +pour obtenir le même objet. + +---- + +![Sous-graphiques](assets/images/eda-plotly-subplot-base.png) + +---- + +#### Propriétés des colonnes et lignes + +Vous pouvez définir les dimensions relatives des colonnes et lignes lors de la création de sous-graphiques. + +```python {.numberLines} +import pandas as pd +from plotly.subplots import make_subplots +from plotly.graph_objs import Figure, Bar + +data = pd.DataFrame(data={ + "product": ["tarte", "gâteau", "biscuit", "mille-feuille", "éclair", "brownie"], + "price": [2.99, 3.49, 1.99, 4.99, 5.99, 6.99], + "weight": [250, 300, 200, 400, 500, 600] +}) +figure: Figure = make_subplots(rows=1, cols=2, column_widths=[0.6, 0.4], row_heights=[1.0]) +figure.add_trace(Bar(name="Prix", x=data["product"], y=data["price"]), row=1, col=1) +figure.add_trace(Bar(name="Poids", x=data["product"], y=data["weight"]), row=1, col=2) +figure.update_layout(template="seaborn", title="Prix et poids unitaires", font={"family": "Cabin", "size": 13}) +figure.show(renderer="browser") +``` + +Ici les colonnes 1 et 2 représentent respectivement 60% et 40% de la largeur totale. + +---- + +![Sous-graphiques avec largeurs de colonnes](assets/images/eda-plotly-subplot-widths-heights.png) + +---- + +### Éléments graphiques + +Vous pouvez agrémenter manuellement vos graphiques d'éléments visuels; les objets `Figure`{.python} +possèdent de nombreuses méthodes pour ajouter des éléments visuels (_images_, _rectangles_, autres…) + +---- + +#### Ajouter une image + +```python {.numberLines} +import pandas as pd +from plotly.graph_objs import Figure, Bar +from PIL import Image + +data = pd.DataFrame(data={ + "product": ["tarte", "gâteau", "biscuit", "mille-feuille", "éclair", "brownie"], + "price": [2.99, 3.49, 1.99, 4.99, 5.99, 6.99], + "weight": [250, 300, 200, 400, 500, 600] +}) +figure: Figure = Figure(data=[Bar(name="Prix", x=data["product"], y=data["price"])]) +figure.add_layout_image( + layer="above", + x=0, + y=1.02, + xref="paper", + yref="paper", + sizex=1, + sizey=0.075, + xanchor="left", + yanchor="bottom", + source=Image.open("images/python-logo-square.png") +) +figure.update_layout(template="seaborn", title="Prix et poids unitaires", font={"family": "Cabin", "size": 13}) +figure.show(renderer="browser") +``` + +**Note** : Nécessite une image valide. + +---- + +![Ajout d'une image de Logo](assets/images/eda-plotly-trace-image.png) + +---- + +#### Options de taille, d'échelle et d'alignement + +| Argument | Type / valeur possible | Description / notes | +|-----------|----------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `xref` | `"paper"` / `"pixel"` / `"x"` | Référence horizontale de l'image. Lorsque `xref` vaut `"paper"`, `x` est relative à la largeur totale du graphique. Lorsque `xref` vaut `"pixel"`, `x` est en pixels. | +| `yref` | `"paper"` / `"pixel"` / `"y"` | Référence verticale de l'image. Lorsque `yref` vaut `"paper"`, `y` est relative à la hauteur totale du graphique. Lorsque `yref` vaut `"pixel"`, `y` est en pixels. | +| `xanchor` | `"auto"` / `"left"` / `"center"` / `"right"` | Alignement horizontal de l'image selon `x`. | +| `yanchor` | `"auto"` / `"top"` / `"middle"` / `"bottom"` | Alignement vertical de l'image selon `y`. | +| `x` | nombre | Position horizontale de l'image (relative à `xref`). | +| `y` | nombre | Position verticale de l'image (relative à `yref`). | +| `sizex` | nombre ∈ [0, 1] | Largeur relative de l'image selon l'axe des X. | +| `sizey` | nombre ∈ [0, 1] | Hauteur relative de l'image selon l'axe des Y. | +| `layer` | `"above"` / `"below"` / `"between"` | Couche de l'image par rapport aux autres éléments du graphique. | +| `source` | `PIL.Image` | Image à afficher. | + +---- + +#### Ajouter un rectangle horizontal + +```python {.numberLines} +import pandas as pd +from plotly.graph_objs import Figure, Bar, Scatter + +data = pd.DataFrame( + data={ + "product": ["tarte", "gâteau", "biscuit", "mille-feuille", "éclair", "brownie"], + "price": [2.99, 3.49, 1.99, 4.99, 5.99, 6.99], + "weight": [250, 300, 200, 400, 500, 600], + } +) +figure: Figure = Figure(data=[Bar(name="Prix", x=data["product"], y=data["price"])]) +figure.add_hrect(y0=2.75, y1=4.5, fillcolor="gray", opacity=0.25, layer="below") +figure.update_layout( + template="seaborn", + title="Prix et poids unitaires", + font={"family": "Cabin", "size": 13}, + xaxis={"title": "Produit", "showgrid": False}, + yaxis={"title": "Prix (€)", "showgrid": False} +) +figure.show(renderer="browser") +``` + +---- + +![Ajout d'un rectangle horizontal](assets/images/eda-plotly-trace-hrect.png) + +---- + +#### Options de rectangle + +| Argument | Type / valeur possible | Description / notes | +|-------------|-------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `y0` | nombre | Position verticale de la ligne en bas (relative à `yref`). | +| `y1` | nombre | Position verticale de la ligne en haut (relative à `yref`). | +| `yref` | `"paper"` / `"pixel"` / `"y"` | Référence verticale de la ligne. Lorsque `yref` vaut `"paper"`, `y` est relative à la hauteur totale du graphique. Lorsque `yref` vaut `"pixel"`, `y` est en pixels. | +| `fillcolor` | `str` (couleur HTML) | Couleur de remplissage de la ligne. | +| `line` | `dict` | Dictionnaire des propriétés de la ligne (voir la documentation de Plotly). | +| `opacity` | `float` ∈ [0, 1] | Opacité de la ligne. | +| `layer` | `"above"` / `"below"` / `"between"` | Couche de la ligne par rapport aux autres éléments du graphique. | ---- diff --git a/documentation/assets/images/eda-plotly-colors-continuous.svg b/documentation/assets/images/eda-plotly-colors-continuous.svg new file mode 100644 index 0000000..843066f --- /dev/null +++ b/documentation/assets/images/eda-plotly-colors-continuous.svg @@ -0,0 +1 @@ +Brwnyl Agsunset Sunsetdark Magenta Sunset Purpor Purp Tealgrn Teal Bluyl Aggrnyl Emrld Darkmint Blugrn Mint Pinkyl Peach Oryel Redor Burgyl Burg tempo amp speed matter algae dense deep gray ice solar haline thermal turbid YlOrRd YlOrBr YlGnBu YlGn Reds RdPu RdBu Purples PuRd PuBuGn PuBu Oranges OrRd Greys Greens GnBu BuPu BuGn Blues Rainbow Jet Hot Electric Bluered Blackbody Turbo Plasma Magma Inferno Cividis Viridis Plotly3 Palette de couleurs continues Plotly Sequential \ No newline at end of file diff --git a/documentation/assets/images/eda-plotly-colors-qualitative.svg b/documentation/assets/images/eda-plotly-colors-qualitative.svg new file mode 100644 index 0000000..10da0f7 --- /dev/null +++ b/documentation/assets/images/eda-plotly-colors-qualitative.svg @@ -0,0 +1 @@ +Vivid Safe Prism Pastel Bold Antique Set3 Pastel2 Set2 Dark2 Pastel1 Set1 Light24 Dark24 Alphabet T10 G10 D3 Plotly Palette de couleurs qualitatives Plotly Qualitative \ No newline at end of file diff --git a/documentation/assets/images/eda-plotly-colors-sequential.svg b/documentation/assets/images/eda-plotly-colors-sequential.svg new file mode 100644 index 0000000..7900748 --- /dev/null +++ b/documentation/assets/images/eda-plotly-colors-sequential.svg @@ -0,0 +1 @@ +Brwnyl Agsunset Sunsetdark Magenta Sunset Purpor Purp Tealgrn Teal Bluyl Aggrnyl Emrld Darkmint Blugrn Mint Pinkyl Peach Oryel Redor Burgyl Burg tempo amp speed matter algae dense deep gray ice solar haline thermal turbid YlOrRd YlOrBr YlGnBu YlGn Reds RdPu RdBu Purples PuRd PuBuGn PuBu Oranges OrRd Greys Greens GnBu BuPu BuGn Blues Rainbow Jet Hot Electric Bluered Blackbody Turbo Plasma Magma Inferno Cividis Viridis Plotly3 Palette de couleurs séquentielles Plotly Sequential \ No newline at end of file diff --git a/documentation/assets/images/eda-plotly-express-bar-colors-custom.png b/documentation/assets/images/eda-plotly-express-bar-colors-custom.png new file mode 100644 index 0000000..7156a89 Binary files /dev/null and b/documentation/assets/images/eda-plotly-express-bar-colors-custom.png differ diff --git a/documentation/assets/images/eda-plotly-express-bar-colors-predefined.png b/documentation/assets/images/eda-plotly-express-bar-colors-predefined.png new file mode 100644 index 0000000..bf04141 Binary files /dev/null and b/documentation/assets/images/eda-plotly-express-bar-colors-predefined.png differ diff --git a/documentation/assets/images/eda-plotly-express-pie-colors-predefined.png b/documentation/assets/images/eda-plotly-express-pie-colors-predefined.png new file mode 100644 index 0000000..34743a7 Binary files /dev/null and b/documentation/assets/images/eda-plotly-express-pie-colors-predefined.png differ diff --git a/documentation/assets/images/eda-plotly-subplot-base.png b/documentation/assets/images/eda-plotly-subplot-base.png new file mode 100644 index 0000000..52989eb Binary files /dev/null and b/documentation/assets/images/eda-plotly-subplot-base.png differ diff --git a/documentation/assets/images/eda-plotly-subplot-widths-heights.png b/documentation/assets/images/eda-plotly-subplot-widths-heights.png new file mode 100644 index 0000000..5cb1c76 Binary files /dev/null and b/documentation/assets/images/eda-plotly-subplot-widths-heights.png differ diff --git a/documentation/assets/images/eda-plotly-trace-hrect.png b/documentation/assets/images/eda-plotly-trace-hrect.png new file mode 100644 index 0000000..518f159 Binary files /dev/null and b/documentation/assets/images/eda-plotly-trace-hrect.png differ diff --git a/documentation/assets/images/eda-plotly-trace-image.png b/documentation/assets/images/eda-plotly-trace-image.png new file mode 100644 index 0000000..dc71303 Binary files /dev/null and b/documentation/assets/images/eda-plotly-trace-image.png differ diff --git a/source/plotting/charts/eda-plotly-colors-continuous.svg b/source/plotting/charts/eda-plotly-colors-continuous.svg new file mode 100644 index 0000000..e396b3d --- /dev/null +++ b/source/plotting/charts/eda-plotly-colors-continuous.svg @@ -0,0 +1 @@ +Brwnyl Agsunset Sunsetdark Magenta Sunset Purpor Purp Tealgrn Teal Bluyl Aggrnyl Emrld Darkmint Blugrn Mint Pinkyl Peach Oryel Redor Burgyl Burg tempo amp speed matter algae dense deep gray ice solar haline thermal turbid YlOrRd YlOrBr YlGnBu YlGn Reds RdPu RdBu Purples PuRd PuBuGn PuBu Oranges OrRd Greys Greens GnBu BuPu BuGn Blues Rainbow Jet Hot Electric Bluered Blackbody Turbo Plasma Magma Inferno Cividis Viridis Plotly3 Palette de couleurs continues Plotly Sequential \ No newline at end of file diff --git a/source/plotting/charts/eda-plotly-colors-qualitative.svg b/source/plotting/charts/eda-plotly-colors-qualitative.svg new file mode 100644 index 0000000..27d82bc --- /dev/null +++ b/source/plotting/charts/eda-plotly-colors-qualitative.svg @@ -0,0 +1 @@ +Vivid Safe Prism Pastel Bold Antique Set3 Pastel2 Set2 Dark2 Pastel1 Set1 Light24 Dark24 Alphabet T10 G10 D3 Plotly Palette de couleurs qualitatives Plotly Qualitative \ No newline at end of file diff --git a/source/plotting/charts/eda-plotly-colors-sequential.svg b/source/plotting/charts/eda-plotly-colors-sequential.svg new file mode 100644 index 0000000..7900748 --- /dev/null +++ b/source/plotting/charts/eda-plotly-colors-sequential.svg @@ -0,0 +1 @@ +Brwnyl Agsunset Sunsetdark Magenta Sunset Purpor Purp Tealgrn Teal Bluyl Aggrnyl Emrld Darkmint Blugrn Mint Pinkyl Peach Oryel Redor Burgyl Burg tempo amp speed matter algae dense deep gray ice solar haline thermal turbid YlOrRd YlOrBr YlGnBu YlGn Reds RdPu RdBu Purples PuRd PuBuGn PuBu Oranges OrRd Greys Greens GnBu BuPu BuGn Blues Rainbow Jet Hot Electric Bluered Blackbody Turbo Plasma Magma Inferno Cividis Viridis Plotly3 Palette de couleurs séquentielles Plotly Sequential \ No newline at end of file diff --git a/source/plotting/charts/images/astral-python.png b/source/plotting/charts/images/astral-python.png new file mode 100644 index 0000000..69c6e4b Binary files /dev/null and b/source/plotting/charts/images/astral-python.png differ diff --git a/source/plotting/charts/images/python-logo-square.png b/source/plotting/charts/images/python-logo-square.png new file mode 100644 index 0000000..ca3a2e5 Binary files /dev/null and b/source/plotting/charts/images/python-logo-square.png differ diff --git a/source/plotting/charts/images/svelte.webp b/source/plotting/charts/images/svelte.webp new file mode 100644 index 0000000..70589d0 Binary files /dev/null and b/source/plotting/charts/images/svelte.webp differ diff --git a/source/plotting/charts/plotly_bar_colors.py b/source/plotting/charts/plotly_bar_colors.py new file mode 100644 index 0000000..9b2386d --- /dev/null +++ b/source/plotting/charts/plotly_bar_colors.py @@ -0,0 +1,17 @@ +import pandas as pd +from plotly import express as px +from plotly.colors import sequential, qualitative + + +df = pd.DataFrame(data={ + "month": [f"2005-{i:02d}" for i in range(1, 13)], + "temperature": [4.4, 6.8, 9.2, 11.5, 13.8, 16.3, 19.1, 21.4, 22.9, 20.5, 17.2, 13.9] +}) +plot = px.bar( + df, x="month", y="temperature", color="temperature", template="seaborn", + title="Temperatures mensuelles de 2005", + color_continuous_scale=sequential.Turbo, + labels={"month": "Mois", "temperature": "Température (°C)"} +) +plot.layout.update({"font": {"family": "Cabin", "size": 13}}) +plot.show() diff --git a/source/plotting/charts/plotly_bar_colors_custom.py b/source/plotting/charts/plotly_bar_colors_custom.py new file mode 100644 index 0000000..a88a102 --- /dev/null +++ b/source/plotting/charts/plotly_bar_colors_custom.py @@ -0,0 +1,17 @@ +import pandas as pd +from plotly import express as px +from plotly.colors import sequential, qualitative + + +df = pd.DataFrame(data={ + "month": [f"2005-{i:02d}" for i in range(1, 13)], + "temperature": [4.4, 6.8, 9.2, 11.5, 13.8, 16.3, 19.1, 21.4, 22.9, 20.5, 17.2, 13.9] +}) +plot = px.bar( + df, x="month", y="temperature", color="temperature", template="seaborn", + title="Temperatures mensuelles de 2005", + color_continuous_scale=["#FF0020", "rgb(255,165,0)", "rgba(255,255,0,0.8)"], + labels={"month": "Mois", "temperature": "Température (°C)"} +) +plot.layout.update({"font": {"family": "Cabin", "size": 13}}) +plot.show() diff --git a/source/plotting/charts/plotly_pie_colors.py b/source/plotting/charts/plotly_pie_colors.py new file mode 100644 index 0000000..cec95c0 --- /dev/null +++ b/source/plotting/charts/plotly_pie_colors.py @@ -0,0 +1,14 @@ +import pandas as pd +from plotly import express as px +from plotly.colors import qualitative + +data = pd.DataFrame(data={ + "name": ["headphones", "microphone", "speaker", "powerbank", "tablet", "laptop"], + "price": [99.99, 49.99, 149.99, 29.99, 199.99, 279.99], + "merchant": ["amazon", "amazon", "bestbuy", "amazon", "newegg", "amazon"], + "color": ["black", "silver", "white", "red", "gray", "silver"] +}) +figure = px.pie(data, values="price", names="name", title="Prix", color_discrete_sequence=qualitative.Bold) +figure.layout.update({"template": "seaborn", "title": "Prix à l'unité", "font": {"family": "Cabin", "size": 13}, "width": 1080, "height": 720}) +figure.update_traces(textposition="inside", textinfo="percent+label", texttemplate="%{label}
%{value}€ (%{percent:.2%})") +figure.show() diff --git a/source/plotting/charts/plotly_subplot_base.py b/source/plotting/charts/plotly_subplot_base.py new file mode 100644 index 0000000..1e93647 --- /dev/null +++ b/source/plotting/charts/plotly_subplot_base.py @@ -0,0 +1,19 @@ +import pandas as pd +from plotly._subplots import SubplotXY +from plotly.subplots import make_subplots +from plotly.graph_objs import Figure, Bar + +data = pd.DataFrame(data={ + "product": ["tarte", "gâteau", "biscuit", "mille-feuille", "éclair", "brownie"], + "price": [2.99, 3.49, 1.99, 4.99, 5.99, 6.99], + "weight": [250, 300, 200, 400, 500, 600] +}) +figure: Figure = make_subplots(rows=1, cols=3, subplot_titles=("Prix", "Poids unitaires")) +subplot = figure.get_subplot(row=1, col=2) +subplot.xaxis["domain"] = [0.3555555, 1.0] +print(subplot, dir(subplot)) +figure.add_trace(Bar(name="Prix", x=data["product"], y=data["price"]), row=1, col=1) +figure.add_trace(Bar(name="Poids", x=data["product"], y=data["weight"]), row=1, col=2) +figure.update_layout(template="seaborn", title="Prix et poids unitaires", font={"family": "Cabin", "size": 13}) +figure.update_traces(row=1, col=2, specs=2) +figure.show(renderer="browser") diff --git a/source/plotting/charts/plotly_subplot_widths.py b/source/plotting/charts/plotly_subplot_widths.py new file mode 100644 index 0000000..4db480a --- /dev/null +++ b/source/plotting/charts/plotly_subplot_widths.py @@ -0,0 +1,14 @@ +import pandas as pd +from plotly.subplots import make_subplots +from plotly.graph_objs import Figure, Bar + +data = pd.DataFrame(data={ + "product": ["tarte", "gâteau", "biscuit", "mille-feuille", "éclair", "brownie"], + "price": [2.99, 3.49, 1.99, 4.99, 5.99, 6.99], + "weight": [250, 300, 200, 400, 500, 600] +}) +figure: Figure = make_subplots(rows=1, cols=2, column_widths=[0.6, 0.4], row_heights=[1.0]) +figure.add_trace(Bar(name="Prix", x=data["product"], y=data["price"]), row=1, col=1) +figure.add_trace(Bar(name="Poids", x=data["product"], y=data["weight"]), row=1, col=2) +figure.update_layout(template="seaborn", title="Prix et poids unitaires", font={"family": "Cabin", "size": 13}) +figure.show() diff --git a/source/plotting/charts/plotly_swatches.py b/source/plotting/charts/plotly_swatches.py new file mode 100644 index 0000000..22c857f --- /dev/null +++ b/source/plotting/charts/plotly_swatches.py @@ -0,0 +1,55 @@ +import plotly.express as px +from plotly.graph_objects import Figure, Scatter + +COLS: int = 3 +fig: Figure = px.colors.sequential.swatches_continuous() +fig3: Figure = px.colors.qualitative.swatches() +figb: Figure = px.colors.sequential.swatches() + +fig2 = Figure(layout={ + "title": "Palette de couleurs continues Plotly Sequential", + "font": {"family": "Cabin", "size": 13}, + "xaxis": {"visible": False, "showticklabels": False}, + "yaxis": {"visible": False, "showticklabels": False}, + "legend": {"visible": False}, + "width": 1400, +}).set_subplots(rows=22, cols=COLS) +for i, p in enumerate(fig.select_traces()): #type: int, Scatter + fig2.add_trace(p, row= (i // COLS) + 1, col=(i % COLS) + 1) +fig2.update_xaxes({"visible": False, "showticklabels": False, "showgrid": False}) +fig2.update_yaxes({"visible": True, "showticklabels": True, "showgrid": False}) +fig2.update_yaxes(tickfont={"size": 11}, ticksuffix=" ") +fig2.write_image("eda-plotly-colors-continuous.svg") +# fig2.show(renderer="browser") + +fig4 = Figure(layout={ + "title": "Palette de couleurs séquentielles Plotly Sequential", + "font": {"family": "Cabin", "size": 13}, + "xaxis": {"visible": False, "showticklabels": False}, + "yaxis": {"visible": False, "showticklabels": False}, + "legend": {"visible": False}, + "width": 1400, + "margin": {"t": 100, "b": 50, "l": 60, "r": 50}, +}).set_subplots(rows=22, cols=COLS) +for i, p in enumerate(figb.select_traces()): #type: int, Scatter + fig4.add_trace(p, row= (i // COLS) + 1, col=(i % COLS) + 1) +fig4.update_xaxes({"visible": False, "showticklabels": False, "showgrid": False}) +fig4.update_yaxes({"visible": True, "showticklabels": True, "showgrid": False}) +fig4.update_yaxes(tickfont={"size": 11}, ticksuffix=" ") +fig4.write_image("eda-plotly-colors-sequential.svg") + +fig3.update_layout({ + "title": "Palette de couleurs qualitatives Plotly Qualitative", + "font": {"family": "Cabin", "size": 13}, + "xaxis": {"visible": False, "showticklabels": False}, + "yaxis": {"visible": False, "showticklabels": False}, + "legend": {"visible": False}, + "width": 1200, + "margin": {"t": 100, "b": 50, "l": 60, "r": 50}, +}) +fig3.update_xaxes({"visible": False, "showticklabels": False, "showgrid": False}) +fig3.update_yaxes({"visible": True, "showticklabels": True, "showgrid": False}) +fig3.update_yaxes(tickfont={"size": 11}, ticksuffix=" ") +fig3.write_image("eda-plotly-colors-qualitative.svg") + + diff --git a/source/plotting/charts/plotly_trace_image.py b/source/plotting/charts/plotly_trace_image.py new file mode 100644 index 0000000..b551a14 --- /dev/null +++ b/source/plotting/charts/plotly_trace_image.py @@ -0,0 +1,30 @@ +import pandas as pd +from plotly.graph_objs import Figure, Bar +from PIL import Image + +data = pd.DataFrame( + data={ + "product": ["tarte", "gâteau", "biscuit", "mille-feuille", "éclair", "brownie"], + "price": [2.99, 3.49, 1.99, 4.99, 5.99, 6.99], + "weight": [250, 300, 200, 400, 500, 600], + } +) +figure: Figure = Figure(data=[Bar(name="Prix", x=data["product"], y=data["price"])]) +figure.add_layout_image( + layer="above", + x=0, + y=1.02, + xref="paper", + yref="paper", + sizex=1, + sizey=0.075, + xanchor="left", + yanchor="bottom", + source=Image.open("images/python-logo-square.png"), +) +figure.update_layout( + template="seaborn", + title="Prix et poids unitaires", + font={"family": "Cabin", "size": 13}, +) +figure.show(renderer="browser") diff --git a/source/plotting/charts/plotly_trace_rectangle.py b/source/plotting/charts/plotly_trace_rectangle.py new file mode 100644 index 0000000..70c6d4b --- /dev/null +++ b/source/plotting/charts/plotly_trace_rectangle.py @@ -0,0 +1,20 @@ +import pandas as pd +from plotly.graph_objs import Figure, Bar, Scatter + +data = pd.DataFrame( + data={ + "product": ["tarte", "gâteau", "biscuit", "mille-feuille", "éclair", "brownie"], + "price": [2.99, 3.49, 1.99, 4.99, 5.99, 6.99], + "weight": [250, 300, 200, 400, 500, 600], + } +) +figure: Figure = Figure(data=[Bar(name="Prix", x=data["product"], y=data["price"])]) +figure.add_hrect(y0=2.75, y1=4.5, fillcolor="gray", opacity=0.25, layer="below") +figure.update_layout( + template="seaborn", + title="Prix et poids unitaires", + font={"family": "Cabin", "size": 13}, + xaxis={"title": "Produit", "showgrid": False}, + yaxis={"title": "Prix (€)", "showgrid": False} +) +figure.show(renderer="browser")