1

for an eink Display its required to use png files with exactly 128x250 pixels.

From a little temperature and humidity logger I get results like:

Date,Time,Temperature,Humidity
12.12.2021,17:02,23.8,48.9
12.12.2021,17:03,22.8,45.1
12.12.2021,17:04,22.7,44.5
12.12.2021,17:05,22.6,44.6
12.12.2021,17:06,22.6,45.4
12.12.2021,17:07,22.5,45.1
13.12.2021,13:02,23.8,48.9
13.12.2021,13:03,22.8,45.1
13.12.2021,13:04,22.7,44.5
13.12.2021,13:05,22.6,44.6
13.12.2021,13:06,22.6,45.4
13.12.2021,13:07,22.5,45.1
14.12.2021,19:02,23.8,48.9
14.12.2021,19:03,22.8,45.1
14.12.2021,19:04,22.7,44.5
14.12.2021,19:05,22.6,44.6
14.12.2021,19:06,22.6,45.4
14.12.2021,19:07,22.5,45.1

With python I did some simple plotting for one day like (I am a nub in python):

from pandas import read_csv as pd
from matplotlib import pyplot as plt

Raspilog = pd('Temp.csv', header=0, squeeze=True)
Raspilog_CDate = Raspilog[Raspilog.Date == "13.12.2021"]


fig, ax = plt.subplots()
ax.plot(Raspilog_CDate.Time, Raspilog_CDate.Temperature, color="red")
ax2 = ax.twinx()
ax2.set(ylim=(0, 100))
ax2.plot(Raspilog_CDate.Time, Raspilog_CDate.Humidity, color="black")

plt.show()

and the result looks like:

enter image description here

But I need it to be 128x250 pixels and saved as png

Ive read all over the internet about DPI and inches and so on. It seems to me, that there is no easy solution :( Does anyone have any idea how to accomplish that?

lilaaffe
  • 127
  • 11
  • 2
    It's not so hard. `pyplot`'s default DPI is 100, so `plt.figure(figsize=(2.5, 1.28) )` should do what you want. – Tim Roberts Dec 17 '21 at 20:11

1 Answers1

2

You need to play around a bit with dpi ('dots per inch': number of pixels in each inch) and the figsize (in inches). The figsize needs to be large enough to make text fig a bit reasonable. Note that 250x128 is extremely small, so reading the text will be hard.

The following code is the result of experimenting with some possible dpis. 50 dpi seems about well.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from io import StringIO

df_str = '''Date,Time,Temperature,Humidity
12.12.2021,17:02,23.8,48.9
12.12.2021,17:03,22.8,45.1
12.12.2021,17:04,22.7,44.5
12.12.2021,17:05,22.6,44.6
12.12.2021,17:06,22.6,45.4
12.12.2021,17:07,22.5,45.1
13.12.2021,13:02,23.8,48.9
13.12.2021,13:03,22.8,45.1
13.12.2021,13:04,22.7,44.5
13.12.2021,13:05,22.6,44.6
13.12.2021,13:06,22.6,45.4
13.12.2021,13:07,22.5,45.1
14.12.2021,19:02,23.8,48.9
14.12.2021,19:03,22.8,45.1
14.12.2021,19:04,22.7,44.5
14.12.2021,19:05,22.6,44.6
14.12.2021,19:06,22.6,45.4
14.12.2021,19:07,22.5,45.1'''
df = pd.read_csv(StringIO(df_str))

png_width, png_height = 250, 128
png_dpi = 50
fig, ax = plt.subplots(figsize=(png_width / png_dpi, png_height / png_dpi))
ax.plot(df.Time, df.Temperature, color="red")
ax2 = ax.twinx()
ax2.plot(df.Time, df.Humidity, color="black")
ax2.set_ylim(0, 100)

ax.set_xticks(np.arange(0, len(df), 2))
ax.set_xticks(np.arange(0, len(df)), minor=True)
ax.set_xticklabels(df.Time[0::2])
ax.tick_params(axis='x', which='both', length=4)
plt.tight_layout()
fig.savefig('Test250x128.png', dpi=png_dpi)

plt.show()

This generates a png of exactly 250x128:

png of 250x128

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Hey Johan, thanks for the nice reply. It looks like somehow a DPI of 50 produces an image with double the pixels on each axis. Changing the "png_dpi" to 100 produces exactly 250x128. Thank you a lot for the help – lilaaffe Dec 19 '21 at 11:55
  • 1
    Did you use `figsize=(png_width / png_dpi, png_height / png_dpi)`? The idea is to find a figsize for which the numbers look a bit decent using the default font sizes). – JohanC Dec 19 '21 at 12:02
  • Hey, yes. "labelsize=7" in the ticks are pretty good :) using DPI=100 – lilaaffe Dec 19 '21 at 12:43
  • 1
    The problem with changing labelsize, is that when you also have titles and axis labels and other texts, it can get complicated. Therefore, the idea behind my code was to use the default label sizes and quickly check the plot on screen. – JohanC Dec 19 '21 at 12:49
  • Yeah, i like your solution very much, makes it easy to change resolutions for me and tinker around. – lilaaffe Dec 19 '21 at 14:19
  • Hey @JohanC, do you actually know how to get the X Axis fixed from 00:00 to 24:00 (whole day) and keep let it plot that way? (i managed to grab only data with specifis date, so only one day will be plotted so far) – lilaaffe Dec 19 '21 at 15:16
  • 1
    You could transform the date plus time to pandas datetime format. And use something like [Python plot with 24 hrs x and y axis using only hours and minutes from timestamp](https://stackoverflow.com/questions/66577395/python-plot-with-24-hrs-x-and-y-axis-using-only-hours-and-minutes-from-timestamp) – JohanC Dec 19 '21 at 15:20
  • Thanks, ive seen that post now. As far i understand, it plots the data and uses the min and max time to set the scale. But can a fixed scale be used? Syntax is incorrect, but what i mean is something like: "ax.set(xlim(00:00, 24:00))" xD – lilaaffe Dec 19 '21 at 15:27
  • 1
    You'd need something like `ax.set_xlim(pd.to_datetime('20211212 00:00'), pd.to_datetime('20211213 00:00'))` – JohanC Dec 19 '21 at 15:33