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
Nothing appears in my graph.
Thank you so much in advance
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()
It seems to me that the main problem is not the plotting but rather the way how you append to your lists. Try replacing get_temperature_bme680
with the code of that function (or post the code of that function so that we don't have to guess). Also, the line line1, = ax.plot(temps, raw_temp_bme, "r")
probably doens't work as raw_temp_bme
doesn't exist already.
Here is a minimal example for how to do the animation: https://stackoverflow.com/a/70439692/12343828
With the updated code and printing the content of the two lists, it is clear what goes wrong: the xlim/ylim needs to be set such that the data is within the plot range. If you insert
ax.set_xlim(min(temps), max(temps))
ax.set_ylim(min(raw_temp_bme), max(raw_temp_bme))
just before fig.canvas.draw()
that should do the trick.
PS.: you also have to correct the variable name in raw_temp.append(...)
.
enter image description here raw_temp_bme
and get_temperature_bme680
is the main error you have to rewrite the program correctly errors are shown in Jupyter Notebook in detail you HVE TO LOOK at the error given in
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.