Save panda boxplot as image

Viewed 9823

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?

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

Related