If I plot with ipython, I automatically see the x/y coordinates when I move with the mouse over the canvas (see bottom right in screenshot):
import matplotlib.pyplot as plt
import numpy as np
my_random = np.random.random(5)
plt.plot(my_random)
plt.show()
How can the same achieved with Pycharm (my plots appear in the SciView toolwindow)?
If not: is there perhaps an easy workaround for it? (and do I have more possibilities if the plot does not appear in the SciView toolwindow?)
using TkAgg it works:
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
my_random = np.random.random(5)
plt.plot(my_random)
plt.show()
Related
I work with matplotlib. When I add the following lines, the figure is not displayed.
import matplotlib
matplotlib.use('Agg')
here is my code :
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plot
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plot.figure(figsize=(12,9))
def convert_sin_cos(x):
fft_axes = fig.add_subplot(331)
y = np.cos(x)
fft_axes.plot(x,y,'g*')
for i in range(3):
fft_axes = fig.add_subplot(332)
x=np.linspace(0,10,100)
fft_axes.plot(x,i*np.sin(x),'r+')
plot.pause(0.1)
convert_sin_cos(x)
Thanks
That's the idea!
When I run your code, the console says:
matplotlibAgg.py:15: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
How can it be useful? When you're running matplotlib code in a terminal with no windowing system, for example: a cluster (running the same code with different inputs, getting lot of results and without the need to move the data I can plot whatever I need).
I want to display the coordinates of my cursor in an image displayed with matplotlib within a Jupyter notebook.
I am using the %matplotlib notebook magic as per this question.
While this provides a nice answer for a static figure, this results in a huge amount of flickering and bugs (the figure sometimes not showing) when used in an interactive setting where the figure is constantly redrawn during slicing. For example,
%matplotlib notebook
from ipywidgets import interact
import matplotlib.pyplot as plt
import numpy as np
vol = np.random.uniform(size=(16, 16, 16))
#interact(z=(0, 15))
def show(z):
plt.imshow(vol[z])
plt.show()
Without %matplotlib notebook, the figure is updating without any flicker, but does not show the cursor coordinates. With the magic, the coordinates are displayed, but the flickering is unbearable.
Is there a way to have pixel coordinates without flickering in that simple situation?
The problem is the use of plt.show(), which will replace the figure. Instead you probably want to update the existing figure.
%matplotlib notebook
from ipywidgets import interact
import matplotlib.pyplot as plt
import numpy as np
vol = np.random.uniform(size=(16, 16, 16))
fig, ax = plt.subplots()
im = ax.imshow(vol[0])
#interact(z=(0, 15))
def show(z):
im.set_array(vol[z])
im.set_clim(vol[z].min(), vol[z].max())
fig.canvas.draw_idle()
Note the the above provides the same functionality as the code in the question, i.e. each array is normalized individually. However, you might decide to set the color normalization only once such that all arrays share the same color limits.
%matplotlib notebook
from ipywidgets import interact
import matplotlib.pyplot as plt
import numpy as np
vol = np.random.uniform(size=(16, 16, 16))
fig, ax = plt.subplots()
im = ax.imshow(vol[0], vmin=vol.min(), vmax=vol.max())
fig.colorbar(im)
#interact(z=(0, 15))
def show(z):
im.set_array(vol[z])
fig.canvas.draw_idle()
I am using jupyter v1.00,Ipython v6.0 and conda v4.3.16 for creating interactive plots. I'm using the following code which is supposed to create one plot and editing it after the change, but it creates multiple plots every time the power variable is changed. why it behaves like this? is it a new thing in Ipython 6.0? I can confirm that it is working in Ipython v5.0
%matplotlib inline
from ipywidgets import interact, IntSlider
import matplotlib.pylab as plt
import numpy as np
power_slider = IntSlider(min=1, max=5)
#interact(power=power_slider)
def plot(power):
plt.figure(figsize=(10, 8))
plt.plot(np.power(range(10), power))
return plt
This works for me:
%matplotlib notebook
from ipywidgets import interact, IntSlider
import matplotlib.pylab as plt
import numpy as np
power_slider = IntSlider(min=1, max=5)
#interact(power=power_slider)
def plot(power):
plt.figure(figsize=(10, 8))
plt.plot(np.power(range(10), power))
return plt
In Pycharm
import matplotlib.pyplot as plt
plt.interactive(True)
plt.plot([1,2,3,4])
The figure showed up and disappeared instantly.
How to set the figure to keep showing?
Since you are in interactive mode the figure will not stay open. You may simply not use interactive mode,
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()
or you may turn it off before showing the figure.
import matplotlib.pyplot as plt
plt.interactive(True)
plt.plot([1,2,3,4])
plt.ioff()
plt.show()
Use plt.show(), i.e.:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()
I am trying to use mpl_toolkits.basemap on python and everytime I use a function for plotting like drawcoastlines() or any other, the program automatically shows the plot on the screen.
My problem is that I am trying to use those programs later on an external server and it returns 'SystemExit: Unable to access the X Display, is $DISPLAY set properly?'
Is there any way I can avoid the plot to be shown when I use a Basemap function on it?
I just want to save it to a file so later I can read it externally.
My code is:
from mpl_toolkits.basemap import Basemap
import numpy as np
m = Basemap(projection='robin',lon_0=0)
m.drawcoastlines()
#m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,120.,10.))
m.drawmeridians(np.arange(0.,360.,60.))
Use the Agg backend, it doesn't require a graphical environment:
Do this at the very beginning of your script:
import matplotlib as mpl
mpl.use('Agg')
See also the FAQ on Generate images without having a window appear.
The easiest way is to put off the interactive mode of matplotlib.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
#NOT SHOW
plt.ioff()
m = Basemap(projection='robin',lon_0=0)
m.drawcoastlines()
#m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,120.,10.))
m.drawmeridians(np.arange(0.,360.,60.))