pydroid showing terminal when importing matplotlib - python

I'm working on a project that creates some graphics using matplotlib in pydroid but I need to see some output in the terminal but when I import matplotlib and run the code it shows the matplotlib GUI window and not the pydroid terminal how can I solve that?
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("/file.csv")
print(df)

It looks like Pydroid 3 (version 5.00_arm) is sensitive to the phrase import matplotlib .
For example , if you run this code:
#import matplotlib
print(3+3)
you won't see any result.
If you want to see the terminal output , first run your code and then use Graphical program output as shown below:

Related

How do I change the backend in CoCalc to show a matplotlib scatter graph?

I have a program that is working fine locally, but I need to use it in CoCalc on the cloud to share with a group. Everything works fine until the end, when I want to create a scatter graph, in standard way. There are a bunch of other specs, but essentially, it's just
plt.scatter(x, y, color = 'blue')
plt.show()
Nothing tricky. But in Cocalc, it tells me "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure."
Here are the imports from the beginning of the file:
import nltk
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
I've searched for this error and tried the solutions I've found, such as importing tkinter. So, for instance, if I change the headers to
import nltk
import tkinter as tk
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
I get a different error: "Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running."
I've seen instructions about pip installations and such, but a) I'm not sure the installations would make everything work for my group, and b) CoCalc generally has most of the standard packages ready to go, so maybe that's not the problem?
Obviously, I'm not experienced with these issues, so help is much appreciated.
If you're using a Jupyter notebook in CoCalc, then the above will just work fine. Here's an example:
https://cocalc.com/wstein/support/matplotlib-scatter-in-jupyter
If you're writing all of your code as part of a Python program (so, e.g., .py files) and running this from a Terminal, CoCalc will also fully support Tk and this working! However, you have to start a new X11 desktop session in CoCalc by clicking +New --> "Linux Graphical X11 Desktop", then run your Python program from the terminal in the upper left. You'll see any windows that pop up in the main window off to the right.
See https://cocalc.com/wstein/support/scatter-matplotlib-x11

How can I display an image on VS Code and PyCharm?

I want to choose the best IDE for Python programming so I'm testing different softwares but I have problem.
When I try this code blocks on VS Code I don't see any error but the image is not showing
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('exit-ramp.jpg')
plt.imshow(image)
VS Code
When I try run the same code on PyCharm I see some errors
PyCharm
But when I run the same code on Jupyter Notebook it works. What can I do?
Bro, try that:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('exit-ramp.jpg')
plt.imshow(image)
plt.show()
i've made a test in my vscode.
Few weeks ago i used a lot pycharm... but i get some library errors with pandas and pycharm its a little bit heavy.
VSCODE its light and i didnt get any library error that i had with pycharm.
I believe this is a backend problem to be changed in matplotlib. If you start your Python shell whether you are in a bash shell or in vscode, you should use a backend that will show the plot. Otherwise the plt.show() call is required.
So, one solution is to change the backend either manually in you Python shell or when starting the Python shell as explained here below.
Note, however, that I always use the qt5 backend but got the same issue as yours: no plot appears. I changed to the Qt5Agg backend and it worked for me (providing PyQt5 is installed).
So, first solution, you can try to set the backend when starting the Python shell:
ipython --pylab Qt5Agg
You should use a GUI backend amongst the possible backend. If you do not known, try to replace Qt5Agg by auto
The second solution consists in changing the backend manually once in the Python shell:
import matplotlib as plt
plt.use('Qt5Agg')
Right click on your code -> Run current file in interactive window.

cannot plot with matplotlib and pandas

I am using OSX (Mojave 10.14.3) and am having a strange issue plotting a pandas (0.24.2) dataframe using matplotlib (3.0.3). I am using python 3.7.3.
So, the code is as:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'name':['john','mary','peter','jeff','bill','lisa','jose'],
'age':[23,78,22,19,45,33,20],
'gender':['M','F','M','M','M','F','M'],
'state':['california','dc','california','dc','california','texas','texas'],
'num_children':[2,0,0,3,2,1,4],
'num_pets':[5,1,0,5,2,2,3]
})
df.plot(kind='scatter',x='num_children',y='num_pets',color='red')
plt.show()
All this does is show an empty window with nothing in it. I was expecting a scatterplot with 7 points. The example is taken from the web tutorial as is.
EDIT
plt.savefig('myfilename.png')
Savefig works.
I am not sure if this will help anyone but I basically had to install python as a framework to make it work. I was using Anaconda, so something like:
conda install python.app
pythonw script.py # note using pythonw
I, then, was able to get the plot to render correctly by using the macosx backend:
import matplotlib as mpl
mpl.use('MacOSX')

Matplotlib window doesn't show up, with TkAgg installed

I run this script in a Jupyter notebook:
import matplotlib.pyplot as plt
plt.plot(range(10))
I get a nice plot as an output. I run this debug.py from terminal:
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()
./debug.py doesn't give a plot. My cursor changes shape, it becomes a cross pointer, but I don't get any plot, and I don't get back my terminal prompt.
Following the recommendation given in this answer, I run in a local python3 shell this:
>>> import matplotlib
>>> print(matplotlib.rcParams['backend'])
The output is TkAgg, as it should be, based on the answer to the aforementioned question.
Why I don't get a matplotlib window then?

Jupyter Notebook - Matplotlib keep running

I just started to use Jupiter Notebook to learn Python. while I am trying out matplotlib with this basic code:
import numpy as np
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
The kernel just keep running and nothing happen. How to resolve this? Is there an dependency issue? My newly installed matplotlib is 1.5.1, python is 3.5.2, numpy is 1.11. Running on MacBook Pro 10.11(El Capitan).
To Visualize the plots created by the matplotlib in Jupiter Notebook or ipython notebook you have add one extra line at the beginning.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
If your matplotlib version is above 1.4, and you are using IPython 3.x you have to use the below code.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
It likely showed you a Matplotlib popup, with the IP(y) logo. To show it inline you have to use some IPython magic. Run this in a cell somewhere:
%matplotlib inline
After you close the popup it will finish the statement in your kernel
It sometimes takes time until the kernel starts.
Check that the code is color-highlighted. If it is, it means that the kernel is running. Evaluate the cell again. You will notice a * beside that cell, meaning it's running.
And one more thing: Sometimes the plot is displayed but its window hides behind the notebook... Access it from the task bar

Categories