UC-MACSS / persp-model_W18

Course site for MACS 30100 (Winter 2018) - Perspectives on Computational Modeling
7 stars 54 forks source link

Getting no output in Python #57

Open gmvelez opened 6 years ago

gmvelez commented 6 years ago

Hello,

I ran the first two chunks of code to input the data for assignment 2 and to get the histogram. When I try to do the next part, it says Out3: <matplotlib.legend.Legend at 0x1a12cfaef0>

This is my code:

def truncnorm_pdf(xvals, mu, sigma, cutoff): if cutoff == 'None': prob_notcut = 1.0 - sts.norm.cdf(0, loc=mu, scale=sigma) else: prob_notcut = (sts.norm.cdf(cutoff, loc=mu, scale=sigma) - sts.norm.cdf(0, loc=mu, scale=sigma))

pdf_vals    = ((1/(sigma * np.sqrt(2 * np.pi)) *
                np.exp( - (xvals - mu)**2 / (2 * sigma**2))) /
                prob_notcut)

return pdf_vals

mu_1 = 11 sig_1 = 0.5 plt.plot(incomes, truncnorm_pdf(incomes, mu_1, sig_1, 15000), linewidth=2, color='r', label='1: $\mu$=11,$\sigma$=0.5') plt.legend(loc='upper left')

Thanks!

cernhofer commented 6 years ago

plt.show() will produce your plot in a separate window on your machine.

gmvelez commented 6 years ago

Thanks for the quick response! When I do that the output line disappears, but nothing opens still. Could it be something with my settings?

cernhofer commented 6 years ago

Ok yes it is probably something to do with how you installed matplotlib. Within that realm, there are a couple of things that could be happening. Let's try looking at the backend configuration of your matplotlib first.

(1) Open your terminal and enter in the following command to get the file and path and name of your matplotlib setup/config file:

(2) Copy the file path and name that comes up and open that file either in a text editor or vim (whichever you'd feel comfortable with)

(3) You need to find where your backend is specified. If your file is the same as the one I have on my machine (which theoretically it is), there should be a comment #### CONFIGURATION BEGINS HERE. The next line of uncommented code is what you want to look at. On my machine it's backend : macosx but I'm guessing that on yours the backend could be set to template. Change it to either macosx (if you have a mac) or another one of the default backends provided (I've heard that GtkAgg is a good option).

(4) Once you've saved the file you should be able to run your program and see your plots.

Let me know if this doesn't work for you- as I said there are a few different things that could be happening here. Also feel free to come to office hours if you're (rightly) confused about this stuff.

gmvelez commented 6 years ago

Thanks for this detailed step by step. When I enter the first line in the terminal, nothing happens.

I might not be able to make it to office hours to figure this out (sorry I didn't realize this yesterday during lab!)

cernhofer commented 6 years ago

Oh yes of course- sorry I forgot to explicitly specify that before you do any of this you have to enter into a python environment in your terminal with the command ipython3 assuming you're using python3 and not a previous iteration. Then import matplotlib and find the filename with the matplotlib.matplotlib_fname() command. Once you have the filepath and name you can exit out of the ipython environment.

gmvelez commented 6 years ago

Thanks - so I went through and checked, and the backend is set to macosx already.

cernhofer commented 6 years ago

Hmm cool. Could you try saving the figure instead of showing it?

plt.savefig('fig.png')

Does this command save the figure for you?