Matplotlib.pyplot.ginput() in Python - python

I am using Jupyter Notebook while running the below code,
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(10)
b = np.sin(a)
plt.plot(a,b)
print("After 3 clicks:")
x = plt.ginput(3)
print(x)
plt.show()
While running this code I get the below warning
UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backend, so cannot show the figure.
x = plt.ginput(3)
Due to this issue, I am not able to click the points on graph nor I am getting the clicked points in output.
The python in my system is of version is 3.9.7 and matplotlib is of version 3.4.3.

The issue is resolved. I used matplotlib.use() before importing pyplot.
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(10)
b = np.sin(a)
plt.plot(a,b)
print("After 3 clicks:")
x = plt.ginput(3)
print(x)
plt.show()
matplotlib.use('Qt5Agg') this changed the non -gui backend to GUI backend of Qt5Agg.

Related

Figure not displayed with matplotlib.use('Agg')

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

Why can't I create a 3d scatter plot in Python on my desktop?

I am trying to use the 3D scatter plot object in python. And I have successfully done this on my laptop. However, I can not copy and paste code onto my desktop. When I do this I get an error. I will attach my the section of my code below that is giving me trouble. I am using Anaconda to run my code. I will note that my laptop uses python 3.6 and my desktop uses 3.7, but I do not think that is causing it. The error I is get is as follows. "ValueError: Unknown projection '3d'"
import numpy as np
from scipy import optimize
import time
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import preprocessing
from sklearn.svm import SVR
import multiprocessing as mp
from obj_class import objective_class
import pdb
import scipy.integrate as integrate
def create3d():
grid_matrix = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3]])
fig = plt.figure()
ax = plt.axes(projection='3d')
p = ax.scatter3D(grid_matrix[:,0],grid_matrix[:,1] ,grid_matrix[:,2] , c=grid_matrix[:,3], cmap='viridis')
cb = fig.colorbar(p)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title(' Scatter Plot')
In order to use a 3d projection in matplotlib <= 3.1 you need to import axes3d, i.e.
from mpl_toolkits.mplot3d import Axes3D
From matplotlib >= 3.2, no extra import is necessary. So possibly you are running different matplotlib versions on both computers.
If you are running your code within an iPython kernel, Jupyter notebook for example,
then you only need to perform each import once and you will be able to run any code which relies on said import until the kernel is shutdown. However, in order to run the script in a self contained fashion you will need that import included in your script.

matplotlib versions 2.2.0 returns error in plt.show

I want to plot but I face some errors
import numpy as np
import matplotlib as plt
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
what is its problem?
cannot find reference 'arange' in __ init__.py
I'm using pycharm on windows 10
is there any difference between matplotlib.py and matplotlib.pyplot?
I can not find the second one
solved: use version 2.1.2
Your import of matplotlib is not correct.
Use import matplotlib.pyplot as plt (notice the extra .pyplot in there)
It'll should run properly.
the problem was with my version of matplotlib
I've installed v.2.2.0. uninstalled it and installed v.2.1.2 then this code start working.
Well first check the versions of your numpy and matplotlib libraries and your code:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
works fine..
It gives me output like this:
So, maybe the problem is with your versions, Moreover, It seems that your numpy package is not installed correctly according to your error. install numpy package and then again run your code.
also add matplotlib.pyplot instead or only matplotlib
Also check this: Matplotlib, Pylab, Pyplot, etc: What's the difference between these and when to use each?
Hope this will help you! :)

Ipython interact function plots multiple plots instead of editing the one

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

matplotlib 1.4.2 with Seaborn: line markers not functioning

Note: this is fixed in 1.4.3 or later
I use the Seaborn plotting package and I just upgraded to the newest version of Matplotlib. Now, plots with dot symbols no longer render. Code that was functional before now creates blank plots, but only when Seaborn is imported. Here's some sample code:
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
print matplotlib.__version__
Matplotlib version:
1.4.2
Create a plot without seaborn:
x = np.linspace(0,2,101)
y = np.sin(2*np.pi*x)
plt.plot(x,y,'.')
Import seaborn, print the version:
import seaborn as sns
print sns.__version__
Seaborn version:
0.4.0
Create a line plot with seaborn imported:
plt.plot(x,y,'-')
Creating a dot plot with seaborn imported gives a blank set of axes:
plt.plot(x,y,'.')
Everything above was done in the IPython notebook, but I just tried the following in Spyder with the same result:
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
print matplotlib.__version__
x = np.linspace(0,2,101)
y = np.sin(2*np.pi*x)
plt.figure()
plt.plot(x,y,'.')
import seaborn as sns
print sns.__version__
plt.figure()
plt.plot(x,y,'-')
plt.figure()
plt.plot(x,y,'.')
plt.show()
What's going on?
It would appear that this is due to a bug in Matplotlib.
https://github.com/matplotlib/matplotlib/issues/3711
https://github.com/mwaskom/seaborn/issues/344
You might just have to downgrade for the time being.
PS: What's up Doug.
A workaround (mentioned in the GitHub links in the other answer) is to explicitly set markeredgewidth (or mew) in the call to plot:
plt.plot(x,y,'.', mew=1)

Categories

Resources