Home > Enterprise >  Python plot save as png with exact 128x250 pixel?
Python plot save as png with exact 128x250 pixel?

Time:12-20

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?

CodePudding user response:

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

  • Related