En Python et Matplotlib, il est facile d'afficher le tracé sous forme de fenêtre contextuelle ou de sauvegarder le tracé en tant que fichier PNG. Comment puis-je sauvegarder l'intrigue dans un tableau numérique au format RVB?Matplotlib: enregistrer un tracé dans un tableau numpy
15
A
Répondre
35
Ceci est une astuce pratique pour les tests unitaires et autres, lorsque vous avez besoin de faire une comparaison de pixel à pixel avec un tracé sauvegardé.
Une façon est d'utiliser fig.canvas.tostring_rgb
puis numpy.fromstring
avec le dtype approprié. Il y a aussi d'autres moyens, mais c'est celui que j'ai tendance à utiliser.
E.g.
import matplotlib.pyplot as plt
import numpy as np
# Make a random plot...
fig = plt.figure()
fig.add_subplot(111)
# If we haven't already shown or saved the plot, then we need to
# draw the figure first...
fig.canvas.draw()
# Now we can save it to a numpy array.
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
Questions connexes
- 1. Tracé de tableau 2D dans matplotlib
- 2. Comment enregistrer un tableau masqué numpy dans un fichier
- 3. Tracé d'image interactif avec matplotlib
- 4. Comment enregistrer un destructeur pour un tableau numpy alloué C?
- 5. Ajouter un tableau NumPy à un tableau NumPy
- 6. Reshape un tableau dans NumPy
- 7. comment enregistrer un tableau booléen numpy à textfile en python?
- 8. remplir un tableau numpy
- 9. Comment annoter/mettre en évidence un tracé 3D dans MatPlotLib
- 10. Interpolation temporelle dans NumPy/matplotlib
- 11. Ajout d'un croisillon ou d'un marqueur à un tracé de contour de matplotlib
- 12. Matplotlib et Numpy Math
- 13. Régression linéaire avec matplotlib/numpy
- 14. Découper un tableau numpy 2D dans un tableau 1D
- 15. Comment mapper un tableau Python dans un tableau NumPy
- 16. Conversion d'un tableau numpy en un tableau d'enregistrement numpy
- 17. Matplotlib: imshow dans le tracé 3D
- 18. Matplotlib: tracé de contour avec widget curseur
- 19. J'arrête (pas en pause) un tracé animé en python (matplotlib)
- 20. Numpy xor-reduce un tableau
- 21. Comment analyser un tableau numpy?
- 22. numpy enregistrer un tableau de différents types dans un fichier texte
- 23. Mélange de patchs de matplotlib avec un tracé polaire?
- 24. Comment faire un tracé 4d en utilisant Python avec matplotlib
- 25. Remodeler un tableau numpy dans python
- 26. Permutation de colonnes dans un tableau numpy?
- 27. Multiplier à travers dans un tableau numpy
- 28. Ajouter un élément unique tableau dans numpy
- 29. ajouter un champ pour enregistrer un tableau?
- 30. affichant un tableau 2d coloré dans matplotlib en Python
Excellent! 12345 – Petter
Est-ce seulement supporté sur certain backend? Ne semble pas fonctionner avec le backend 'macosx' (' tostring_rgb') introuvable. – mirosval
Fonctionne sur Agg, ajoute 'matplotlib.use ('agg')' avant d'importer matplotlib.pyplot comme plt' pour l'utiliser. – mirosval