Matplotlib installation

I want to plot my analog sensor output using matplotlib but while trying to install it I am getting errors.

Update your pip3 using the command

sudo -H pip3 install --upgrade pip

After that you may install matplotlib for python 3 using the command

sudo apt-get install python3-matplotlib
1 Like

Thank you, matplotlib installation was successful, but I could not plot using it.
from matplotlib import pyplot as plt
plt.plot(x)

Could you attach the python script you are using? If there are any API keys in your code, please hide them.

I have only bolt API

You need to convert the response which is a json object appropriately such as by using

data=json.loads(response)
if(data["success"]==1):
  actual_val=int(data["value"])

You may then use plt.plot(actual_val)

Also, please remove the second image in your previous post and reset you API keys immediately.

still getting errors

Store the values of actual_val to a list by appending into it everytime new value is read.

list_name=[ ]
while True:
.....
  if(....):
    ....
    list_name.append(actual_val)
    plt.plot(list_name)
    plt.show()

At the end plot the list and use plt.show(list_name) to display the plot. The above method may lead to opening multiple plot windows.

still getting the same error

Sorry, I didn’t realise you were using a server edition on your virtual machine. To view the plot you’ll require a GUI, whereas you are using a command line.
You may either use Windows or follow the below procedure to install a Desktop environment for Ubuntu.

https://phoenixnap.com/kb/how-to-install-a-gui-on-ubuntu

I’ll advise you use either Gnome 2, unity or xfce if you want to use Ubuntu.

A cleaner way would be to download the desktop version of ubuntu and install it on your virtual machine, but the above method should also work.

Note: please remove the images with API keys in your previous posts and reset the keys if you haven’t.

@pankajkumar.p Then, I think Raspberry Pi would be a better option. The same code can run in Raspberry Pi, right?

Any system that has a GUI and support for matplotlib/python should work just fine.

Operating systems for Raspberry Pi such as the official Raspbian OS also have CLI(minimal) and GUI(full) versions.

I tried it using raspberry pi and plot window appeared but there is no plot inside it
please help me

dd ds

Did you get multiple plots or just a single plot?

single plot window with no graph

Print actual_val to see if you’re getting anything on the terminal.

did plt.plot(actual_val) without Matplotlib installation
still got the same blank plot

Try something like this and check the output on the terminal

list_name=[ ]
while True:
.....
  if(....):
    ....
    list_name.append(actual_val)
    print(list_name)
    plt.plot(list_name)
    plt.show()

@pankajkumar.p only 1st reading is being printed and plot window appears without any graph as before
ds

@swethatsunil.mec,

To plot the graph there must be at-least two data points in the list . You can check this code https://repl.it/join/rkkbzovt-rahulsingh1

Since there is no display environment available thats why output will be save in graph.png file.