2

My problem in general: I have a function, that creates and saves the histograms. In my code I run the function twice: 1st time to create and save one plot with one data array, 2nd time to create and save second plot with another data array. After the completion of the program, I get 2 .png files: the 1st one contains the histogram of one data array, the 2nd one contains histogram of the first AND the second data arrays! What I need is one plot for one array, and second plot for another array. My mind's gonna blow, I just can't get, what's wrong here. Might somebody give me a clue?

Here's a part of my code and resulting images:

    def mode(station_name, *args):
        ...
        #before here the 'temp' data array is generated

        temp_counts = {}

        for t in temp:
            if t not in temp_counts:
                temp_counts[t] = 1
            else:
                temp_counts[t] += 1

        print(temp_counts)  **#this dictionary has DIFFERENT content being printed in two function runs**

        x = []
        for k, v in temp_counts.items():
            x += [k for _ in range(v)]

        plt.hist(x, bins="auto")

        plt.grid(True)

        plt.savefig('{}.png'.format(station_name))

    #---------------------------------------------------------------------------------------------------
    mode(station_name, [...])
    mode(station_name, [...])

the 'like' of 1 image i get

the 'like' of 2 image i get

real images i get after my full script finishes #1

real images i get after my full script finishes #2

1

1 Answer 1

1

If you use plt.plotsomething.. the plot is added to the current figure in use, therefore the second histogram is added to the first. I suggest using the matplotlib object API to avoid confusion: you create figure and axis and you generate your plots starting from them. Here's your code:

def mode(station_name, *args):
    ...
    #before here the 'temp' data array is generated

    temp_counts = {}

    for t in temp:
        if t not in temp_counts:
            temp_counts[t] = 1
        else:
            temp_counts[t] += 1

    print(temp_counts)  **#this dictionary has DIFFERENT content being printed in two function runs**

    x = []
    for k, v in temp_counts.items():
        x += [k for _ in range(v)]

    fig, ax = plt.subplots(1):
    ax.hist(x, bins="auto")
    ax.grid(True)
    fig.savefig('{}.png'.format(station_name))

#---------------------------------------------------------------------------------------------------
mode(station_name, [...])
mode(station_name, [...])

This should do the job for you

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, this have solved my problem. Aslo, I found out, that closing figure in use with plt.close('all') after saving could do the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.