0

First I want to generate a plot, colorized by how close the average data is to the goal. (the real program is a machine learning algorithm trying to learn weights). I want to generate a histogram for each data point in the final plot, but I can't seem to generate the plots independently. Any help would be greatly appreciated.

import matplotlib.pyplot as plt
import numpy as np

def generateData(goal):
    x=[_ for _ in range(20)]
    y=[10+np.random.exponential()*5 for _ in range(100)]
    return x,y

def drawHistogram(data,nBins):
    plt.figure(2)
    plt.hist(diffs,nBins)
    plt.draw()
    plt.show()

sweep=np.linspace(10,20,4)

for goal in sweep:
    for gw2 in sweep:

        diffs=[]

        for i in range(10):
            data=generateData(goal)
            diffs.append(goal-np.mean(data[1]))


        #generate plot
        plt.figure(1)
        clr=(abs(np.mean(diffs))/goal,0,0)
        plt.plot([goal], [gw2], marker="s", mew='1', ms='35', color=clr)

        drawHistogram(diffs,5) ##Comment this line out to see what the final graph should look like

plt.figure(1)
plt.draw()
plt.show()
8
  • plt.figure(2) creates a new figure. Is that desired or not? Commented Mar 8, 2018 at 17:06
  • As an example, if you would like to create a plot for each iteration of the loop, you could do: for g, goal in enumerate(sweep): plt.figure(g+1) Commented Mar 8, 2018 at 17:08
  • @ImportanceOfBeingErnest yes - I want to basically append to figure(1) and to create a histogram, figure(2), for each of those points. Commented Mar 8, 2018 at 17:12
  • The problem is when I run this code, when I call plt.draw() and plt.show() in drawHistogram(), it draws both figures instead of only the histogram. I only want to draw figure(1) at the end. Commented Mar 8, 2018 at 17:13
  • Can you be more specific as to exactly when a new plot needs to be generated? What do you mean by appending to figure(1)? FYI plt.show() will display all figures. Commented Mar 8, 2018 at 17:46

1 Answer 1

1

This generates 17 individual histograms and 1 final figure with the red squares.

import matplotlib.pyplot as plt
import numpy as np

def generateData(goal):
    x=[_ for _ in range(20)]
    y=[10+np.random.exponential()*5 for _ in range(100)]
    return x,y

def drawHistogram(data,nBins):
    plt.figure()
    plt.hist(diffs,nBins)

sweep=np.linspace(10,20,4)

for goal in sweep:
    for gw2 in sweep:

        diffs=[]

        for i in range(10):
            data=generateData(goal)
            diffs.append(goal-np.mean(data[1]))

        #generate plot
        plt.figure(1)
        clr=(abs(np.mean(diffs))/goal,0,0)
        plt.plot([goal], [gw2], marker="s", mew='1', ms='35', color=clr)

        drawHistogram(diffs,5) ##Comment this line out to see what the final graph should look like

plt.draw()
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

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.