I have started with vispy but I cannot plot a line of data.
I see a lot of examples which have used package but that doesn't work either.
I have a white window and my line does not appear, why?
This is my code:
import numpy as np
from vispy import plot as vp
fig = vp.Fig(size=(600, 500), show=True)
x=[0,1,2,3,4,5,6]
y=x
line = fig[0, 0].plot((x, y), width=3, color='k')
fig.show()
Can you try:
fig.show(run=True)
It works on my system. Otherwise, it might comes from the pyqt version. I think python 3.6 use pyqt 5 and I'm not sure that vispy is compatible with this version of Qt. If it doesn't works, try to create a python 3.5 environment (it should have pyqt 4 installed). Then re-installed vispy an try your example.
Related
I was using this code on my Jupyter Notebook until yesterday and it was working fine:
%matplotlib qt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X,Y,Z,c='black')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
This morning, for no apparent reason, it started saying that there was an error with qt4 or something. After some research I installed qt5 and changed the first line of the code to "%matplotlib qt5" which now makes the code run without errors, but doesn't generate any graphs. If I take the 5 after qt I run into the message "Warning: Cannot change to a different GUI toolkit: qt. Using qt5 instead.". I can still plot graphs with "%matplotlib inline", but I wanted some interactivity. Any ideas why this is happening?
It seems something got updated; but with the information available it's not possible to find out what it is. In any case, since you have pyqt5 installed you may use %matplotlib qt5. Then you probably just forgot to type plt.show()?
%matplotlib qt5
import matplotlib.pyplot as plt
plt.plot([1,4,2])
plt.show()
Also make sure to restart the kernel if you change the interactive backend, and if in doubt, let the line %matplotlib qt5 appear before importing pyplot.
I'm trying to make GTK3 and Python3 work under windows to my project.
I have an continuum anaconda setup with a 32-bit python 3.4 and Matplotib via
conda install matplotlib.
I've installed PyGobject(https://sourceforge.net/projects/pygobjectwin32/) and installed GTK+ / Glade via the installer.
The basic exemple from the GTK3 tutorial works well (empty screen)
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
I want now to embed matplotlib in gtk, I got the example from matplotlib (http://matplotlib.org/examples/user_interfaces/embedding_in_gtk3.html)
I saw then that I needed cairocffi because some incompabilities. (PyCairo has no support for a matplotlib function)
I got the windows binaries for cffi from Gohlke
And finnaly did a
pip install cairocffi
And now I just get a python.exe stopped working.
Tried with GTK3agg and GTK3Cairo backends and I have the same result
Looking around I found that maybe the cairo version is outdated for the functions used by matplotlib, but I dont know how to proceed.
Cairocffi works if I try running something else.
More information (from the comment below):
I still got an unhandled win32 error. I managed to open the error and it says:
Unhandled exception at 0x08CF6D58 (libcairo-2.dll) in python.exe:
0xC0000005: Access violation reading location 0x000000A8.
If there is a handler for this exception, the program may be safely continued.
It just crashes...
I've had my share of problems using matplotlib in Python3 + Gtk3. I found this cookbook page with working examples. Try to run the examples in the cookbook - particularly the simplest one:
#!/usr/bin/python3
from gi.repository import Gtk
from matplotlib.figure import Figure
from numpy import arange, pi, random, linspace
import matplotlib.cm as cm
#Possibly this rendering backend is broken currently
#from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
myfirstwindow = Gtk.Window()
myfirstwindow.connect("delete-event", Gtk.main_quit)
myfirstwindow.set_default_size(400, 400)
fig = Figure(figsize=(5,5), dpi=100)
ax = fig.add_subplot(111, projection='polar')
N = 20
theta = linspace(0.0, 2 * pi, N, endpoint=False)
radii = 10 * random.rand(N)
width = pi / 4 * random.rand(N)
bars = ax.bar(theta, radii, width=width, bottom=0.0)
for r, bar in zip(radii, bars):
bar.set_facecolor(cm.jet(r / 10.))
bar.set_alpha(0.5)
ax.plot()
sw = Gtk.ScrolledWindow()
myfirstwindow.add(sw)
canvas = FigureCanvas(fig)
canvas.set_size_request(400,400)
sw.add_with_viewport(canvas)
myfirstwindow.show_all()
Gtk.main()
Also, not that you need a fairly recent version of matplotlib to make things work on Python3.
If you still have problems, please show us the complete error message.
Note: I tested this on Linux (don't have Windows), but, from you description of problems, the issue is (was) a common one.
I have the same issue on Windows since years. The documentation of matplotlib 2.0.0 realease states that Gtk3 backend is not supported on Windows. Recently, I had an issue running the Gtk3Agg backend under Linux (Ubuntu). In both cases, it is always related to Cairo.
Thus, I wrote my own implementation of this backend, you can find it here. import the FigureCanvasGtk3Agg from my module and use it the same way as the official one. It lacks some features, but, if you just want to display a plot, it'll do the job.
You can try the module by running it, it should display a simple colorful graph in a window. I tried it under both Linux and Windows and had no issue.
How does it works:
The trick is to avoid importing Cairo in Python3 as it usually doesn't work (to my experience). It is done by using a GdkPixbuf.
Then Gdk.cairo_set_source_pixbuf does the rest of the job along with calling two methods of the cairo context provided by Gtk in the 'draw-event' callback. Cairo is never imported in the module.
Have recently started playing with Python. I have installed Python Spyder app on the Mac. Everything has been working well until recently for some reason the matplotlib charts stopped displaying. A simple code like this does not work anymore:
import matplotlib.pyplot as plt
x = np.linspace(0.4 * np.pi, 100)
plt.plot(x)
The only output is
[]
I have Spyder 2.3.3, Python 3.4.3 64 Bit.
You have to tell the plot to show itself with:
plt.show()
I just installed matplotlib in Ubuntu 9.10 using the synaptic package system.
However, when I try the following simple example
>>> from pylab import plot;
>>> plot([1,2,3],[1,2,3])
[<matplotlib.lines.Line2D object at 0x9aa78ec>]
I get no plot window. Any ideas on how to get the plot window to show?
You can type
import pylab
pylab.show()
or better, use ipython -pylab.
Since the use of pylab is not recommended anymore, the solution would nowadays be
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
pylab.show() works but blocks (you need to close the window).
A much more convenient solution is to do pylab.ion() (interactive mode on) when you start: all (the pylab equivalents of) pyplot.* commands display their plot immediately. More information on the interactive mode can be found on the official web site.
I also second using the even more convenient ipython -pylab (--pylab, in newer versions), which allows you to skip the from … import … part (%pylab works, too, in newer IPython versions).
Try this:
import matplotlib
matplotlib.use('TkAgg')
BEFORE import pylab
The code snippet below works on both Eclipse and the Python shell:
import numpy as np
import matplotlib.pyplot as plt
# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)
# Just print x and y for fun
print x
print y
# Plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)
# Without the line below, the figure won't show
plt.show()
Any errors show up? This might an issue of not having set the backend. You can set it from the Python interpreter or from a config file (.matplotlib/matplotlibrc) in you home directory.
To set the backend in code you can do
import matplotlib
matplotlib.use('Agg')
where 'Agg' is the name of the backend. Which backends are present depend on your installation and OS.
http://matplotlib.sourceforge.net/faq/installing_faq.html#backends
http://matplotlib.org/users/customizing.html
Modern IPython uses the "--matplotlib" argument with an optional backend parameter. It defaults to "auto", which is usually good enough on Mac and Windows. I haven't tested it on Ubuntu or any other Linux distribution, but I would expect it to work.
ipython --matplotlib
If you encounter an issue in which pylab.show() freezes the IPython window (this may be Mac OS X specific; not sure), you can cmd-c in the IPython window, switch to the plot window, and it will break out.
Apparently, future calls to pylab.show() will not freeze the IPython window, only the first call. Unfortunately, I've found that the behavior of the plot window / interactions with show() changes every time I reinstall matplotlib, so this solution may not always hold.
If you are starting IPython with the --pylab option, you shouldn't need to call show() or draw(). Try this:
ipython --pylab=inline
--pylab no longer works for Jupyter, but fortunately we can add a tweak in the ipython_config.py file to get both pylab as well as autoreload functionalities.
c.InteractiveShellApp.extensions = ['autoreload', 'pylab']
c.InteractiveShellApp.exec_lines = ['%autoreload 2', '%pylab']
If you are user of Anaconda and Spyder then best solution for you is that :
Tools
-->
Preferences
-->
Ipython console
-->
Graphic Section
Then in the Support for graphics (Matplotlib) section:
select two avaliable options
and in the Graphics Backend:
select Automatic
Another possibility when using easy_install is that you need to require the most recent version of matplotlib.
Try:
import pkg_resources
pkg_resources.require("matplotlib")
before you import matplotlib or any of its modules.
I'm having trouble getting the window for matplotlib to show.
I've downloaded python 3.3, matplotlib for python 3.3, and numpy.
I've also installed python tools for Visual Studio 2012 so I can create python solutions in that environment.
With all that out of the way... I'm running this EXTREMELY simple script:
import numpy as np
import matplotlib.pyplot as plt
import pylab
# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)
# plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)
# without the line below, the figure won't show
pylab.show()
This compiles with no warnings or errors, but only my console window displays; no graph or interactive window ever shows up. I tried running the scrip from the command prompt thinking maybe the visual studio environment was throwing it off, but it still didn't work.
I also tried running with python 2.7 and it also didn't work.
Every tutorial I found confirmed that this should be working. I'm pulling my hair out and would praise any help at this point.
You should type
plt.show()
instead.
You need to put
plt.figure()
to make the figure window before you start plotting information to it
If you are using a GUI console such as Spyder, make sure you have turned on interactive plots so that it plots to a separate window and not inline into the console