| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente |
| python:matplotlib [2023/07/10 17:45] – [Légendes] precise alpha for a hatched legend item phsw | python:matplotlib [2024/10/06 15:47] (Version actuelle) – [Matplotlib] add link phsw |
|---|
| |
| * [[https://hal.inria.fr/hal-03427242/document|Scientific Visualization: Python + Matplotlib]] | * [[https://hal.inria.fr/hal-03427242/document|Scientific Visualization: Python + Matplotlib]] |
| | * [[https://duetosymmetry.com/code/latex-mpl-fig-tips/|Fonts/sizes in matplotlib figures for LaTeX publications]] |
| | * https://datavizcatalogue.com/FR/ |
| | |
| |
| |
| </code> | </code> |
| |
| Paramètres facultatifs de ''[[https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html|ax.legend()]]'': | Paramètres facultatifs de ''[[https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html|ax.legend()]]'' : |
| * ''handletextpad'': espacement entre le symbole et le texte | * ''handletextpad'' : espacement entre le symbole et le texte |
| * ''borderpad'': padding de la boîte de légende | * ''borderpad'' : padding de la boîte de légende |
| * ''borderaxespad'': décalage par rapport aux bordures du graphique | * ''borderaxespad'' : décalage par rapport aux bordures du graphique |
| * ''handlelength'': longueur du symbole | * ''handlelength'' : longueur du symbole |
| * ''ncol'': sur combien de colonnes répartir les éléments de la légende | * ''labelspacing'' : espace entre deux éléments de la légende (par défaut 0,5) |
| * ''prop={'size': 6}'' ou ''fontsize'': change la taille du texte de la légende (accepte un entier ou les valeurs ''xx-small'', ''x-small'', ''small'', ''medium'', ''large'', ''x-large'', ''xx-large'' | * ''ncol'' : sur combien de colonnes répartir les éléments de la légende |
| * ''title="Un titre"'': donne un titre à la légende ([[https://matplotlib.org/stable/gallery/text_labels_and_annotations/legend_demo.html#sphx-glr-gallery-text-labels-and-annotations-legend-demo-py|source]]) | * ''prop={'size': 6}'' ou ''fontsize'' : change la taille du texte de la légende (accepte un entier ou les valeurs ''xx-small'', ''x-small'', ''small'', ''medium'', ''large'', ''x-large'', ''xx-large'' |
| | * ''title="Un titre"'' : donne un titre à la légende ([[https://matplotlib.org/stable/gallery/text_labels_and_annotations/legend_demo.html#sphx-glr-gallery-text-labels-and-annotations-legend-demo-py|source]]) |
| |
| La taille de la police de la légende peut être définie par défaut: | La taille de la police de la légende peut être définie par défaut: |
| # or: | # or: |
| plt.xticks([]) # also hide grid lines | plt.xticks([]) # also hide grid lines |
| | </code> |
| | |
| | Changer l'emplacement des graduations et leurs valeurs (depuis une version récente de Matplotlib) : |
| | <code python> |
| | plt.yticks(ticks, labels) |
| | # ou |
| | ax.set_yticks(ticks, labels) |
| </code> | </code> |
| |
| for tick in ax.get_xticklabels(): | for tick in ax.get_xticklabels(): |
| tick.set_rotation(45) | tick.set_rotation(45) |
| | |
| | # or (with Matplotlib >= 3.5): |
| | ax.set_xticks(ax.get_xticks(), ax.get_xticklabels(), rotation=45, ha='right') |
| |
| # Pad margins so that markers don't get clipped by the axes | # Pad margins so that markers don't get clipped by the axes |
| for i in range(len(bars)): | for i in range(len(bars)): |
| bars[i].set_hatch(hatches[i]) | bars[i].set_hatch(hatches[i]) |
| | # pour avoir un export correct dans les PDFs, utiliser plutôt : |
| | bars[i].set(hatch="//", alpha=1) |
| </code> | </code> |
| |
| </code> | </code> |
| |
| | |
| | |
| | === Boxplots === |
| | |
| | Pour que Matplotlib calcule directement les statistiques : ''ax.boxplot()'' ([[https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.boxplot.html#__do_not_save__|documentation]]) |
| | |
| | Pour fournir nous-mêmes les statistiques à Matplotlib : ''ax.bxp()'' ([[https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bxp.html#__do_not_save__|documentation]]) |
| | |
| | |
| | == Changer la couleur de fond == |
| | |
| | [[https://matplotlib.org/stable/gallery/statistics/boxplot_color.html#sphx-glr-gallery-statistics-boxplot-color-py|Source]] |
| | |
| | <code python> |
| | boxplot = ax1.boxplot(all_data, patch_artist=True) |
| | for b in boxplot['boxes']: |
| | b.set_facecolor(color) |
| | </code> |
| | |
| | == Légende correspondante == |
| | |
| | [[https://stackoverflow.com/questions/47528955/adding-a-legend-to-a-matplotlib-boxplot-with-multiple-plots-on-same-axes|Source]] |
| | |
| | <code python> |
| | boxplot = ax.boxplot(all_data, patch_artist=True) |
| | ax.legend([boxplot["boxes"][0]], ["Label"]) |
| | </code> |
| |
| |