lehrkraefte:blc:csv-python-matplotlib

CSV import

Siehe auch https://docs.python.org/3/library/csv.html

import csv
 
with open('daten.csv', 'r') as myFile:
    reader = csv.DictReader(myFile)
    myList = [dictionary for dictionary in reader]
 
# Erste Zeile:
print(myList[0])
umwandlung = {"schlecht": 0, "neutral": 1, "gut":2}
spalte = "Wie geht es dir?"
for zeile in myList:
   zeile[spalte] = umwandlung[zeile[spalte]]

Paare zählen

tage = 30
stufen = 5
anzahl = [[0 for s in range(stufen)] for t in range(tage)]
spalte1 = "Tagnummer"
spalte2 = "Wie geht es dir?"
for zeile in myList:
    anzahl[zeile[spalte1]][zeile[spalte2]] += 1

Erstellen der Grafik

Siehe https://matplotlib.org/stable/plot_types/basic/scatter_plot.html#sphx-glr-plot-types-basic-scatter-plot-py

und https://matplotlib.org/cheatsheets/

import matplotlib.pyplot as plt
import numpy as np
 
# Syntax 
# [value outerloop innloop]
 
xpositions = [t for t in range(tage) for s in range(stufen)]
ypositions = [s for t in range(tage) for s in range(stufen)]
markersize = [anzahl[t][s] for t in range(tage) for s in range(stufen)]
 
fig, ax = plt.subplots()
 
ax.scatter(xpositions, yposition, s=markersize)
 
ax.set(xlim=(0, tage), xticks=np.arange(1, tage),
       ylim=(0, staufen), yticks=np.arange(1, stufen))
 
plt.show()
fig.savefig("meinegrafik.pdf")
  • lehrkraefte/blc/csv-python-matplotlib.txt
  • Last modified: 2024/11/27 16:25
  • by Ivo Blöchliger