简体   繁体   中英

Matplotlib.pyplot - how to save a histogram in a variable for later access?

Due to data access patterns, I need to save various histograms in a Python list and then access them later to output as part of a multi-page PDF.

If I save the histograms to my PDF as soon as I create them, my code works fine:

def output_histogram_pdf(self, pdf):
        histogram = plt.hist(
            x=[values], bins=50)
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        plt.title(title)

        if isinstance(pdf, PdfPages):
            pdf.savefig()

But if I instead save them to a list so I can later manipulate the order, I run into trouble.

histogram_list.append(histogram)

Then later

for histogram in histogram_list:
            plt.figure(histogram)
            pdf.savefig()

This does not work. I'm either saving the wrong thing, or I don't know how to properly open what I've saved.

I've spent quite some time fruitlessly googling for a working solution, but so many of the terms involved are sufficiently vague that I get tons of different types of issues in my search results. Any help would be greatly appreciated, thanks!

Short Answer

You can use plt.gcf()

When creating your graph, after setting xlabel, ylabel, and title, append the figure to histogram list.

histogram_list.append(plt.gcf())

You can then iterate over the list later and call savefig.

Long Answer

plt.hist doesn't return the figure object. However, the figure object can be obtained using gcf (Get Current Figure).

In case you do not want to use the current figure, you could always create the figure yourself, using plt.figure or plt.subplot .

Either way, since you are already plotting the histogram and setting the labels for the figure, you'd want to append the figure to the list.

Option 1: using gcf

    histogram = plt.hist(
        x=[values], bins=50)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.title(title)
    histogram_list.append(plt.gcf())

Option 2: create your own figure

    figure = plt.figure(figsize=(a,b,))
    # draw histogram on figure
    histogram_list.append(figure)

Each histogram is formed by (n,bins,patches) where n are the values for each bin, bins are the bins edges (1 more than n), and patches are the artists to create the bars.

Most simply, try to plot each histogram as

for histogram in histogram_list:
    n = histogram[0]
    bins = histogram[1]
    plt.plot(bins[:-1], n, '-', ds='steps-pre')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM