Home > other >  Python matplotlib plot temperature and humidity from csv
Python matplotlib plot temperature and humidity from csv

Time:01-08

If somebody is used to matplotlib and knows how to achieve the desired result, id be very happy.

It could be useful for somebody else too :)

background: A humidity and temperture sensor is logging my data into a csv file, which may look like that (example extract):

Date,Time,Temperature,Humidity
11.12.2022,10:00,18.7,63.1
11.12.2022,11:00,18.7,63.2
11.12.2022,12:00,18.8,63.0
11.12.2022,13:00,18.8,63.1
11.12.2022,14:00,18.7,63.1
11.12.2022,15:00,18.7,62.7
11.12.2022,15:00,18.8,62.6
11.12.2022,16:00,18.7,62.7
11.12.2022,17:00,18.7,62.8
11.12.2022,18:00,18.7,62.5
11.12.2022,19:00,18.7,62.6
11.12.2022,20:00,18.7,62.9
11.12.2022,21:00,18.7,62.5
12.12.2022,10:00,18.7,63.1
12.12.2022,11:00,18.7,63.2
12.12.2022,12:00,18.8,63.0
12.12.2022,13:00,18.8,63.1
12.12.2022,14:00,18.7,63.1
12.12.2022,15:00,18.7,62.7
12.12.2022,15:00,18.8,62.6
12.12.2022,16:00,18.7,62.7
12.12.2022,17:00,18.7,62.8
12.12.2022,18:00,18.7,62.5
12.12.2022,19:00,18.7,62.6
12.12.2022,20:00,18.7,62.9
12.12.2022,21:00,18.7,62.5

Im trying to plot that data on a E-Ink Dislplay which needs an exact resolution of pixels. A little success is already made. The plot shall contain only data from a specific date (usually current date, but in code below i used a test-date variable)

So far i am getting following result:

current state:

But my desired result shall be more like that (but pretty ofc):

desired state:

The labels on the right hand side should contain Date, time, temp and humidity of the last measurement taken (last line of the log file, because with each new logging the csv log file just grows from top to bottom)

The Python code i scrabmled together looks like that(probably not pretty, but somehow works):

import pandas as pd
from matplotlib import pyplot as plt

# for testing:
testdate = "12.12.2022"
# local logger.csv file:
Raspilog = pd.read_csv(r'path/to/logfile/logger.csv', header=0, squeeze=True)  # Logfile read
# for testing with testdate, only data from testdate is considered for plot:
Raspilog_CDate = Raspilog[Raspilog.Date == testdate]

# Plot log data
fig, ax = plt.subplots(figsize=(2.50, 1.28))  # Display size at DPI 100 (default is 100)
# Temp plot
ax.plot(Raspilog_CDate.Time, Raspilog_CDate.Temperature, color=(0.93, 0.11, 0.14), linewidth=3)
# Y-Axis Ticks and Labels create
ax.set(ylim=(-10, 30))
# Hum Plot as twin from Temp and merged
ax2 = ax.twinx()
ax2.plot(Raspilog_CDate.Time, Raspilog_CDate.Humidity, color="black", linewidth=1.5)  # schwarze Linie für Luftfeuchtigkeit
ax2.set(ylim=(0, 100))

# Ticks for X-axis
t = [Raspilog_CDate.Time.min(), Raspilog_CDate.Time.max()]
plt.xticks(t, t)

plt.tight_layout(pad=0, w_pad=0.5, h_pad=1.0)
ax.xaxis.set_tick_params(labelsize=8)
ax.yaxis.set_tick_params(labelsize=8)
ax2.yaxis.set_tick_params(labelsize=8)

# Display current date:
plt.text(0.5, 0.9, testdate, horizontalalignment='center', verticalalignment='center', transform=ax.transAxes,
         fontsize=8)  # Datum vom Messtag als Label


# plt.savefig('logplot.png')
plt.show()

Is somebody able to explain me, how i get the plot squeezed a little bit to have room for text labels on the right hand side?

CodePudding user response:

You can easily add an annotation on the side in axes co-ordinates and make space using layout='constrained'

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(layout='constrained')

ax.plot(np.arange(10))
ax.text(1.03, 0.9,
        'this is some annotations\n\nboo', transform=ax.transAxes)
plt.show()

enter image description here

  • Related