Just a small question. I have VS Code installed and trying it out with Python but no matter what I try I cannot get matplotlib plots to appear.
Here is a simple code that does NOT work
import mglearn
import matplotlib.pyplot as plt
X, y = mglearn.datasets.make_wave(n_samples=40)
plt.plot(X, y, 'o')
plt.ylim(-3, 3)
plt.xlabel("Feature")
plt.ylabel("Target")
No error with the code appears but also no plot. Thanks.
Please note mglearn comes from the following Github
https://github.com/amueller/mglearn
Try plt.show() at the end.
And this additional line is just because the system asks me extra text for no reason.
Your code works.
You need to ask for the picture to be shown plt.show() or to be saved plt.savefig().
Just add plt.show() and you will obtain:
Related
https://www.youtube.com/watch?v=pl3D4SosO_4&list=PL9mhv0CavXYjiIniCLj_5KKN58PaxJBVj&index=2
Right at time stamp 2:13.
I am trying to follow a youtube tutorial and I have hit a road block. There is no documentation online that tells me how to implement matplotlib.collections.PathCollection
The youtuber in this video that I am following runs the first bit of his code (at about 2:13) and a plot appears with some color data points. Above this plot it says <matplotlib.collections.PathCollection at 0x208cd62cef0>
If anyone could tell me how this youtuber got this plot to appear I would be forever grateful.
I have found documentation for matplotlib.collections, but zero information on how it is used, I asked the youtuber how he got to this point in the comments and am waiting on an answer.
Thank you Craig, I am adding the code that doesn't work for me here
EDIT:
(I have been attempting this in a pycharm IDE and the video is using Jupyter, idk if that makes a difference)
import numpy as np
from matplotlib import plyplot as plt
from sklearn.datasets import make_blobs
X,y = make_blobs(n_samples = 500, centers = 5, random_state = 3)
plt.figure(0)
plt.grid(True)
plt.scatter(X[:,0], X[:,1],c=y)
when this is run in the video a plot with color clusters appears.
Maybe I should be trying this in jupyter, maybe some image libraries are pre-loaded there?
Jupyter is a special interactive environment and it automatically renders matplotlib plots when it runs a cell that creates a plot. If you are doing the same thing in an IDE, then you will need to explicitly render the plot by calling plt.show() when you want the plot to appear. You can do this for your code by adding it to the end:
import numpy as np
from matplotlib import plyplot as plt
from sklearn.datasets import make_blobs
X,y = make_blobs(n_samples = 500, centers = 5, random_state = 3)
plt.figure(0)
plt.grid(True)
plt.scatter(X[:,0], X[:,1],c=y)
plt.show() # <-- show the plot
I have a small graph plotting code written in python using matplotlib and the graph is not being displayed output. The script works fine. I have added print statements at the end and it works fine, however the graph doesn't show up. Can you please help?
Following is the code snippet I have used for plotting:-
import matplotlib.pyplot as plt
plt.title("BPD")
ax.set_xlabel('D')
ax.set_ylabel('P')
plt.savefig("DP.png")
plt.pause(5)
plt.show(block== False)
plt.close()
Remove plt.pause(5), plt.show(block== False) and plt.close() from your code
When I call:
model = doKMeans(user3, 4)
and then
ax.scatter(model.cluster_centers_[:,1], model.cluster_centers_[:,0],
s=169, c='r', marker='x', alpha=0.8, linewidths=2)
and then:
showandtell("Weekday Calls Centroids")
my chart appears empty. Any ideas why this is happening?
I believe the issue there is as simple as adding
plt.show()
to the end of your code.
Alternatively, another common mistake that might be causing that is that you if you are working on a jupyter notebook you might have forgot to add the command:
%matplotlib inline
when you import your matplotlib library
Hope that helps!
I want to create a log-log plot using pyplot, but have trouble when calling plt.show():
import matplotlib.pyplot as plt
xVec = [...]
yVec = [...]
plt.figure()
plt.loglog(xVec,yVec,'.',label='This is my test plot')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.show()
I am running this code from C++ via:
Py_Initialize();
Py_SimpleString(pythonCode.str().c_str());
Py_Exit(0);
where pythonCode is a stringstream containing the Python code above. The code runs if I don't include the plt.show() line, but of course no plot shows up.
The matplotlibrc config file shows that the backend is TkAgg, which shouldn't give problems as indicated here or here. I've tried adding plt.close() after the last line in the code above, but the error persists.
Perhaps the most surprising thing is this: I've also tried running the code in a separate Python script (with plt.show()), and the plot appears correctly! Does anyone have any idea about what's going on? Thanks in advance!
EDIT: I have also tried pylab instead of pyplot, with the same results. Do I need to compile the program with a certain python module to link the libraries properly?
I keep trying to follow the examples I see for PdfPages but keep getting the value error: No such figure: None.
plot1 = Chart Generating Function(argument1, argument2,...)
from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('sample.pdf')
pp.savefig(plot1)
plt.close()
I've tried different variations of this (i.e. pdf.savefig()) but nothing seems to work.
What solved the problem for me was removing the plt.show() command at the end of my chart generating function.
I should have added more details, but somewhere in my code I had used "fig, ax = ..." when defining the figure. The "fig" part needed to be the argument in pdf.savefig(fig) in order for it to work.