matplotlib does not show plot() - python

I tried running plt.show() but no plot is shown. I had tried lots of solutions from stackoverflow including setting matplotlib backend to Qt4Agg, switching to other backends (i.e. TkAgg, Agg) and reinstalling matplotlib package but still not solving the issue. Some solutions that I had tried but not working are:
matplotlib does not show my drawings although I call pyplot.show()
Matplotlib.Pyplot does not show output; No Error
Why matplotlib does not plot?
Matplotlib does not show labels or numbers
Below is the code that I tried to run:
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel = "age"
plt.ylabel = "friends count"
plt.title = "Facebook Friends count by Age"
plt.show()
When plt.scatter(pf["age"], pf["friend_count"]) code was run, a scatter plot was shown but with no labels and title. Running plt.show() did not plot and no error was shown. Appreciate any help.
I installed Anaconda with Python 3 for Windows OS. My Mac laptop is running on Bootcamp.

The labels must use parentheses such as plt.xlabel("text"), not assigned to a string with = like you have in your example code. Make the changes in your code, save the changes, quit and reopen Spyder or whatever interpreter you are running, then run the code again.
plt.figure(1)
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel("age")
plt.ylabel("friends count")
plt.title("Facebook Friends count by Age")
plt.show()

Related

plt.show() does not display anything

I am using Jupyter notebook and I'm trying to plot graphs using subplots based on this video:
I just tried everything in the same format as shown in the video.
When I do a simple plot,
For example,
plt.plot([1,2,3],[2,4,6])
It gives a normal result.
But, when I try to create two axis, like this,
ax1.plot(df.index, df['APT.AX'])
ax2.plot(df.index, df['A2M.AX'])
It just gives the following result:
Out: [<matplotlib.lines.Line2D at 0x1ab0e0db8b0>]
And when I enter plt.show(), it just does nothing.
I tried uninstalling and reinstalling matplotlib using conda. I have tried by adding %matplotlib inline before using it.
Anything I can do here?
That's not an error. Use show() in the next line or plt.show()

Pyplot shows image without plt.show()

I had a look at other questions on that regard, but in all other cases people had
%matplotlib inline
which caused plots to show without being prompted. I don't have it.
My plotting code looks like this:
fig, ax = plt.subplots(figsize=(17,8))
ax = plt.hist(results_df['Diff'], bins=100, density=True, histtype='step')
plt.savefig('backtester_results/figures/rf_model_lag_{n_days}_data_{lag}_lag.png',
format='png')
It's an output from testing machine learning model I'm working on and I want to put it to run for 40 hours and there will be few hundred of these plots generated in the process. So I really want them not to just get saved in a folder and not take up memory.
Do you know how to suppress this behaviour or what could be causing it?
I'm using Python 3.6 and Spyder editor. In Spyder settings I have Graphics backend set up as 'Automatic'.

Different plots with show and savefig

I am plotting histograms with quite large number of bins. I am using Spyder, Python 3.6.3. The problem I found is that the figure I am seeing in iPython console is NOT the same as the saved plots. I have seen this thread which asks a similar question, however, my problem is worse, as it's not just the fonts and sizes that vary, I am actually getting different counts!
E.g. the same script:
plt.clf()
fig, ax=plt.subplots()
fig.dpi=100
plt.hist(df['POS'], bins=nbins, range=(0,dict_l[c]))
plt.savefig('current_chr17.jpg', dpi=100)
will produce this plot as a saved figure:
and show this in iPython console (interactive mode on, I'm not even asking for plt.show):
Does anyone have any explanation as to what is going on?

Matplotlib Graph not showing up on OSX?

I am currently trying to graph data using Python3 and matplotlib. I am developing on OSx Sierra and when I run, it is not showing up. There are no errors returned. I included plt.show() in my code and it is definitely running. Any help to get this graph showing would be appreciated. Vanilla Python3, edited in Emacs, ran from both IDLE and terminal. Neither work. Thank you.
import matplotlib.pyplot as plt
plt.show()
Does not produce anything, and there are no errors. I have tried
plt.switch_backend('MacOSX') and the error persists.
Just to wrap up the comments into an answer: pyplot.show() only produces a figure, if a figure has been created. This can be done explicitly by stating
fig = plt.figure()
or implicitly by plotting something (the figure is then created in the background), e.g.
plt.plot([1,2,3])
once plt.show() is called, all currently active figures will be displayed on the screen.

Cartopy: Can't add features to geo axes (no errors or warnings)

I can't add features to cartopy geo axes. Here is an example from the gallery:
import cartopy
import matplotlib.pyplot as plt
def main():
ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.add_feature(cartopy.feature.LAKES, alpha=0.5)
ax.add_feature(cartopy.feature.RIVERS)
ax.set_extent([-20, 60, -40, 40])
plt.show()
if __name__ == '__main__':
main()
When I run this on my laptop at home I get nothing but a blank geo axes in the figure (I can tell because the extents are correct and I can see the coordinates in the bottom left hand corner of the figure window. BUT - if I run this same example at work, everything plots as expected in the link.
My feeling is that there is some kind of dependency issue, but I'm not sure where to start with this one. There are absolutely no warnings or errors of any kind.
Both at work and on my laptop I am running Windows 7 x64 with Python 2.7 and installed cartopy via windows binaries from here
If I plot something myself like a contour plot then it does show up, but I think there is something going wrong between getting the data from naturalearthddata.com, processing the shape files, and adding them to the axes.
Does anyone have any ideas on where to start with this one?
I wasn't getting this error previously, but when I ran the code from an IPython notebook for some reason I got this error HTTPError: HTTP Error 404: Not Found
What I ended up having to do was change line 264 in shapereader.py from:
_NE_URL_TEMPLATE = ('http://www.nacis.org/naturalearth/{resolution}'
'/{category}/ne_{resolution}_{name}.zip')
To:
_NE_URL_TEMPLATE = ('http://www.naturalearthdata.com/'
'http//www.naturalearthdata.com/download/{resolution}'
'/{category}/ne_{resolution}_{name}.zip')
Which was recommended in multiple places in the past including here
It has been mentioned that cartopy has since fixed this broken link to naturalearth in the latest release, but I think the link is not updated in the version of cartopy that I have installed via the mentioned windows binary. I think this issue is fairly well documented, but for some reason I could not see what the actual error was up until now.

Categories