Personnaliser les préférences

Nous utilisons des cookies pour vous aider à naviguer efficacement et à exécuter certaines fonctions. Vous trouverez ci-dessous des informations détaillées sur tous les cookies sous chaque catégorie de consentement.

Les cookies classés comme « Nécessaires » sont stockés sur votre navigateur car ils sont essentiels pour activer les fonctionnalités de base du site.... 

Toujours actif

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Aucun cookie à afficher.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

Aucun cookie à afficher.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Aucun cookie à afficher.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Aucun cookie à afficher.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

Aucun cookie à afficher.

Comment exporter un tracé Matplotlib avec un arrière-plan transparent



Vous pouvez utiliser la syntaxe de base suivante pour exporter un tracé Matplotlib avec un arrière-plan transparent :

savefig('my_plot.png', transparent=True)

Notez que l’argument par défaut de savefig() est transparent=False .

En spécifiant transparent=True nous pouvons enregistrer une figure Matplotlib avec un fond transparent.

L’exemple suivant montre comment utiliser cette syntaxe dans la pratique.

Exemple : exporter un tracé Matplotlib avec un arrière-plan transparent

Le code suivant montre comment créer un tracé linéaire dans Matplotlib et enregistrer le tracé avec un arrière-plan transparent :

import matplotlib.pyplot as plt

#define x and y
x = [1, 4, 10, 15]
y = [5, 7, 21, 22]

#create line plot
plt.plot(x, y)

#add title and axis labels
plt.title('Title of Plot')
plt.xlabel('X Label')
plt.ylabel('Y Label')

#save plot with transparent background
plt.savefig('my_plot.png', transparent=True)

Si je navigue jusqu’à l’emplacement sur mon ordinateur où l’image est enregistrée, je peux la visualiser :

Cependant, cela n’illustre pas bien le fond transparent.

Pour cela, je peux placer l’image sur un fond coloré dans Excel :

Notez que l’arrière-plan est complètement transparent.

Vous pouvez comparer cela avec exactement la même image enregistrée sans utiliser l’argument transparent :

#save plot without specifying transparent background
plt.savefig('my_plot2.png')

L’arrière-plan est blanc, qui est la couleur d’arrière-plan par défaut dans Matplotlib.

Remarque : Vous pouvez trouver la documentation complète en ligne de la fonction savefig() ici .

Ressources additionnelles

Les didacticiels suivants expliquent comment effectuer d’autres opérations courantes dans Matplotlib :

Comment enregistrer la figure Matplotlib dans un fichier
Comment augmenter la taille du tracé dans Matplotlib
Comment créer plusieurs tracés Matplotlib sur une seule figure

Ajouter un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *