lehrkraefte:blc:csv-python-matplotlib

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

lehrkraefte:blc:csv-python-matplotlib [2024/11/27 16:16] – created Ivo Blöchligerlehrkraefte:blc:csv-python-matplotlib [2024/11/27 16:25] (current) Ivo Blöchliger
Line 1: Line 1:
 +====== CSV import ======
 +Siehe auch https://docs.python.org/3/library/csv.html
 +
 +<code python>
 +import csv
 +
 +with open('daten.csv', 'r') as myFile:
 +    reader = csv.DictReader(myFile)
 +    myList = [dictionary for dictionary in reader]
 +
 +# Erste Zeile:
 +print(myList[0])
 +</code>
 +
 +===== Umwandlung in Zahlen =====
 +<code python>
 +umwandlung = {"schlecht": 0, "neutral": 1, "gut":2}
 +spalte = "Wie geht es dir?"
 +for zeile in myList:
 +   zeile[spalte] = umwandlung[zeile[spalte]]
 +</code>
 +
 +Paare zählen
 +
 +<code python>
 +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
 +</code>
 +
 +====== 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/
 +
 +<code python>
 +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")
 +</code>