Why can I not plot using Python on repl.it

2024/7/4 15:31:10

For practical reasons, I want to test a small piece of Pyton code on repl.it (webbased, so I do not need to install Python).

The code

import numpy as np
import matplotlib.pyplot as plttime = np.array([0, 1, 2, 3])
vec = np.array([1, 4, 3, -2])plt.plot(time, vec)

gives following error:

[GCC 4.8.2] on linuxTraceback (most recent call last):File "main.py", line 16, in <module>plt.figure()File "/goval_modules/python35/matplotlib/pyplot.py", line 535, in figure**kwargs)File "/goval_modules/python35/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_managerreturn new_figure_manager_given_figure(num, figure)File "/goval_modules/python35/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figurewindow = Tk.Tk()File "/usr/local/lib/python3.5/tkinter/__init__.py", line 1868, in __init__self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
exited with non-zero status

What does this mean?

Answer

matplotlib by default picks an interactive backend, showing the plot in a GUI; the default is to use TkInter. See What is a backend in the matplotlib FAQ. That's not going to work when Python is run on a webserver, there is no GUI server to display this on.

You'll have to pick a non-interactive backend; Agg is supported and produces PNGs. Repl.it allows you to save files 'locally' (*):

import matplotlib as mpl
mpl.use('Agg')
import numpy as np
import matplotlib.pyplot as plttime = np.array([0, 1, 2, 3])
vec = np.array([1, 4, 3, -2])plt.plot(time, vec)
plt.savefig('graph.png')

When you run this, an extra graph.png tab appears that lets you download the result. See the live demo; the above produces:

matplotlib graph


(*) For new scripts, enable project mode first by clicking on the new file icon. It looks like a document-with-plus-sign and sits underneath the 'settings' cog icon, above the line numbers of the editor.

https://en.xdnf.cn/q/120068.html

Related Q&A

Pull Data from web link to Dataframe [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Python program that rolls a fair die counts the number of rolls before a 6 shows up

import randomsample_size = int(input("Enter the number of times you want me to roll the die: "))if (sample_size <=0):print("Please enter a positive number!")else:counter1 = 0coun…

How to read CSV file in Python? [duplicate]

This question already has answers here:How do I read and write CSV files?(9 answers)Closed 1 year ago.Im using Spyder for Python 2.7 on Windows 8. Im trying to open and read a csv file and see all the…

How to match the bundle id for android app?

Id like to match the urls like this:input: x = "https://play.google.com/store/apps/details?id=com.alibaba.aliexpresshd&hl=en"get_id(x)output: com.alibaba.aliexpresshdWhat is the best way…

Memory Usage During running a Deep learning CNN Model in Colab

I am conducting a research which requires me to know the memory used during run time by the model when i run a deep learning model(CNN) in google colab. Is there any code i can use to know the same .Ba…

Gensim example, TypeError:between str and int error

When running the below code. this Python 3.6, latest Gensim library in Jupyterfor model in models:print(str(model))pprint(model.docvecs.most_similar(positive=["Machine learning"], topn=20))[1…

How to interpret this JSON file?

Im trying to interpret this JSON file but I couldnt figure it out. {"results": [{"fsq_id": "4dc586fbcc3ff3b3045e2ef3","categories": [{"id": 17056,"…

How to extract a field from this payload with a regex? [duplicate]

This question already has answers here:Parse JSON with Python(2 answers)Closed 6 years ago.I have this payload that I wish to extract a field from:{"encrypted_sender_transaction_id":"514…

Python reading xml

I am newbie on Python programming. I have requirement where I need to read the xml structure and build the new soap request xml by adding namespace like here is the example what I have Below XML which …

How can sum two nested list in this situation

Given list a, b a=[[[1.1,-2.1],[-0.6,4.2]],[[3.9,1.3],[-1.3,1.2]]]b=[[-1.1,4.3],[-1.4,2.4]]If I just want to sum the list [[1.1,-2.1],[-0.6,4.2]] in the list a (not the whole list a) with the list [-1.…