Pandas Plot x-Achse DateTime unvollständig
Verfasst: Dienstag 5. Dezember 2023, 15:40
Hallo zusammen,
ich habe eine CSV-Datei in der das Datum, die Batteriespannung und der Ladezustand, etc. enthalten sind.
ich möchte nun von der gleichen Datei verschiedene Plot erstellen, z.B. letzten 7 Tage, letzer Monat,Jahr.
Wenn ich das ganze Jahr auswähler funktioniert der Plot, bei den letzten 7 Tagen fehlt an der x-Achse die Minuten und das Jahr, das finde ich unleserlich und man muss erst raten was es bedeuten soll. Ich möchte bei allen Auswertungen das Format %Y-%m-%d %H:%M haben. Wie kann ich das einstellen ?
Das Script lautet:
#!/bin/python3
#
# Examples in https://www.w3schools.com/python/pandas
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime
import pandas as pd
import matplotlib.pyplot as plt
file="/home/t759f/akku-werte1-neu.txt"
def get_num_lines(fname):
with open(fname) as f:
for i, _ in enumerate(f):
pass
return i + 1
num_lines = get_num_lines(file)
#print(num_lines)
#n = 288 # print last x lines (1 Tag)
n = 220 # print last x lines (1 Tag)
df=pd.read_csv(file, header=0, sep=';',
usecols= ['DateTime','battery_voltage','percent_remain','capacity_remain',
'current_charge','current_discharge'], skiprows=range(1,num_lines-n))
df.DateTime=pd.to_datetime(df['DateTime'])
dx = pd.DataFrame(df, columns=['DateTime'])
print(dx)
df.info()
df.head(3)
#df.DateTime=pd.to_datetime(df['DateTime'])
#pd.options.display.max_rows = 7 # one week
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(df['DateTime'], df['battery_voltage'], label="battery_voltage")
ax.plot(df['DateTime'], df['percent_remain'], label="percent_remain")
plt.xticks(rotation=30, ha='right')
plt.subplots_adjust(bottom=0.3,left=0.057,right=0.917,top=0.9)
plt.legend()
plt.show()
#plt.savefig("/tmp/output.png", dpi=300)
Input:
DateTime
0 2023-12-03 15:12:00
1 2023-12-03 15:22:00
2 2023-12-03 15:28:00
3 2023-12-03 15:38:00
4 2023-12-03 15:48:00
.. ...
215 2023-12-04 15:08:00
216 2023-12-04 15:13:00
217 2023-12-04 15:19:00
218 2023-12-05 09:14:00
219 2023-12-05 09:19:00
[220 rows x 1 columns]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 220 entries, 0 to 219
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 DateTime 220 non-null datetime64[ns]
1 battery_voltage 220 non-null float64
2 percent_remain 220 non-null int64
3 capacity_remain 220 non-null float64
4 current_charge 220 non-null float64
5 current_discharge 220 non-null float64
dtypes: datetime64[ns](1), float64(4), int64(1)
memory usage: 10.4 KB
https://privat.bb-24.net/s/PosZoe2AdSFM6jj
Auswertung nach entfernen von skiprows=range(1,num_lines-n) :
https://privat.bb-24.net/s/o3qkX57oXpqfJyk
Sorry, ich habe es nicht hinbekommen hier ein Bild einzufügen.
ich habe eine CSV-Datei in der das Datum, die Batteriespannung und der Ladezustand, etc. enthalten sind.
ich möchte nun von der gleichen Datei verschiedene Plot erstellen, z.B. letzten 7 Tage, letzer Monat,Jahr.
Wenn ich das ganze Jahr auswähler funktioniert der Plot, bei den letzten 7 Tagen fehlt an der x-Achse die Minuten und das Jahr, das finde ich unleserlich und man muss erst raten was es bedeuten soll. Ich möchte bei allen Auswertungen das Format %Y-%m-%d %H:%M haben. Wie kann ich das einstellen ?
Das Script lautet:
#!/bin/python3
#
# Examples in https://www.w3schools.com/python/pandas
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime
import pandas as pd
import matplotlib.pyplot as plt
file="/home/t759f/akku-werte1-neu.txt"
def get_num_lines(fname):
with open(fname) as f:
for i, _ in enumerate(f):
pass
return i + 1
num_lines = get_num_lines(file)
#print(num_lines)
#n = 288 # print last x lines (1 Tag)
n = 220 # print last x lines (1 Tag)
df=pd.read_csv(file, header=0, sep=';',
usecols= ['DateTime','battery_voltage','percent_remain','capacity_remain',
'current_charge','current_discharge'], skiprows=range(1,num_lines-n))
df.DateTime=pd.to_datetime(df['DateTime'])
dx = pd.DataFrame(df, columns=['DateTime'])
print(dx)
df.info()
df.head(3)
#df.DateTime=pd.to_datetime(df['DateTime'])
#pd.options.display.max_rows = 7 # one week
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(df['DateTime'], df['battery_voltage'], label="battery_voltage")
ax.plot(df['DateTime'], df['percent_remain'], label="percent_remain")
plt.xticks(rotation=30, ha='right')
plt.subplots_adjust(bottom=0.3,left=0.057,right=0.917,top=0.9)
plt.legend()
plt.show()
#plt.savefig("/tmp/output.png", dpi=300)
Input:
DateTime
0 2023-12-03 15:12:00
1 2023-12-03 15:22:00
2 2023-12-03 15:28:00
3 2023-12-03 15:38:00
4 2023-12-03 15:48:00
.. ...
215 2023-12-04 15:08:00
216 2023-12-04 15:13:00
217 2023-12-04 15:19:00
218 2023-12-05 09:14:00
219 2023-12-05 09:19:00
[220 rows x 1 columns]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 220 entries, 0 to 219
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 DateTime 220 non-null datetime64[ns]
1 battery_voltage 220 non-null float64
2 percent_remain 220 non-null int64
3 capacity_remain 220 non-null float64
4 current_charge 220 non-null float64
5 current_discharge 220 non-null float64
dtypes: datetime64[ns](1), float64(4), int64(1)
memory usage: 10.4 KB
https://privat.bb-24.net/s/PosZoe2AdSFM6jj
Auswertung nach entfernen von skiprows=range(1,num_lines-n) :
https://privat.bb-24.net/s/o3qkX57oXpqfJyk
Sorry, ich habe es nicht hinbekommen hier ein Bild einzufügen.