Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save panda boxplot as image

I'm trying to save a pandas.DataFrame.boxplot variable to a image to use it with a Qt widget, but I don't know how to convert this variable. I have this code:

import matplotlib.pyplot as plt
from pandas import DataFrame
import numpy as np


df = DataFrame(np.random.rand(10,5))
plt.figure();
bp = df.boxplot()

And Spyder shows it:

Are there instructions to do it automatically within the code?

like image 905
Daniel Garcia Avatar asked May 29 '17 14:05

Daniel Garcia


2 Answers

Suppose you have multiple figures and you want to save them independently when you want in the code: make sure you can access it by a unique name:

fig100 = figure()        
outputBoxplot100 = df_100.boxplot(column=['1', '2', '4', '5', '8'])
plt.title("100 MHz")
fig150 = figure()
outputBoxplot150 = df_150.boxplot(column=['1', '2', '4', '5', '8'])
plt.title("150 MHz")

# do other stuff

fig100.savefig("test100.svg", format="svg")
fig150.savefig("test150.svg", format="svg")

In this case, I would change your code in:

import matplotlib.pyplot as plt
from pandas import DataFrame
import numpy as np


df = DataFrame(np.random.rand(10,5))
myFig = plt.figure();
bp = df.boxplot()
myFig.savefig("myName.svg", format="svg")

The result will be a saved file named "myName.svg":

enter image description here

like image 80
Leos313 Avatar answered Sep 20 '22 05:09

Leos313


Are you looking for a standard image format?

If so this will do the trick:

import matplotlib.pyplot as plt
plt.savefig()

docs: https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.savefig.html

like image 40
Andrew L Avatar answered Sep 22 '22 05:09

Andrew L