Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting no output in Python #57

Open
gmvelez opened this issue Jan 18, 2018 · 7 comments
Open

Getting no output in Python #57

gmvelez opened this issue Jan 18, 2018 · 7 comments

Comments

@gmvelez
Copy link

gmvelez commented Jan 18, 2018

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
Copy link
Collaborator

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

@gmvelez
Copy link
Author

gmvelez commented Jan 18, 2018

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
Copy link
Collaborator

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:

  • matplotlib.matplotlib_fname()

(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)

  • Text editor: open filepath/filename (this should just open your default text editor depending on how your machine is set up)
  • Vim: vim filepath/filename

(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
Copy link
Author

gmvelez commented Jan 18, 2018

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
Copy link
Collaborator

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
Copy link
Author

gmvelez commented Jan 18, 2018

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

@cernhofer
Copy link
Collaborator

cernhofer commented Jan 18, 2018

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

plt.savefig('fig.png')

Does this command save the figure for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants