PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, June 27, 2022

[FIXED] How to refresh plot in Python

 June 27, 2022     graph, matplotlib, python     No comments   

Issue

I have some issue to do a graph and I was wondering if someone could help me please. Here is my situation :

I've got two arrays : one for temperature values and another for the time.

My goal is to do a graph representing the temperature in function of time that refresh automatically each time a value is appended in my arrays (every 0.2 seconds).

Here is a code that I tried but it didn't work at all. I'm not at my ease at all with Python and plot so sorry for all these mistakes

import matplotlib.pyplot as plt
import random
import time

# My arrays are empty at the beginning
raw_temp_bme = []
temps = []

plt.ion()

fig, ax = plt.subplots(figsize=(8, 6))
line1, = ax.plot(temps, raw_temp_bme, "r") 

plt.title("Température du BME680 en fonction du temps")
plt.xlabel("Temps en ms")
plt.ylabel("Température en °C")

start_time = time.time()

while True:
    # I append my two arrays
    raw_temp.append(round(random.uniform(15.0, 30.0), 2))
    temps.append(round(time.time() - start_time, 2))

    line1.set_xdata(temps)
    line1.set_ydata(raw_temp_bme)
    fig.canvas.draw()
    fig.canvas.flush_events()
    time.sleep(0.2)

Here is a screenshot of what I've got screenshot of my graph

Nothing appears in my graph.

Thank you so much in advance


Solution

Probably your data are just plotted outside the view that is not autoscaled. Minimal example:

import matplotlib.pyplot as plt
import random
import time

# My arrays are empty at the beginning
raw_temp_bme = [30]
temps = [0]

plt.ion()
fig, ax = plt.subplots(figsize=(8, 6))
line1, = ax.plot(temps, raw_temp_bme, "r") 

#simulating your sensor data
def get_temperature_bme680():
    x =  random.random()
    temps.append(temps[-1] + x)  
    raw_temp_bme.append(raw_temp_bme[-1] + 2 * random.random() - 1)
    if len(temps) > 50:
        temps[:] = temps[1:]
        raw_temp_bme[:] = raw_temp_bme[1:] 
    time.sleep(x)
     

while True:
    get_temperature_bme680() # my fonction to append my arrays
    line1.set_xdata(temps)
    line1.set_ydata(raw_temp_bme)
    ax.set_xlim(min(temps), max(temps))
    ax.set_ylim(min(raw_temp_bme), max(raw_temp_bme))
    fig.canvas.draw()
    fig.canvas.flush_events()
    time.sleep(0.0002)

plt.show()


Answered By - Mr. T
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing