Differences
This shows you the differences between two versions of the page.
| lehrkraefte:blc:csv-python-matplotlib [2024/11/27 16:16] – created Ivo Blöchliger | lehrkraefte:blc:csv-python-matplotlib [2024/11/27 16:25] (current) – Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== CSV import ====== | ||
| + | Siehe auch https:// | ||
| + | |||
| + | <code python> | ||
| + | import csv | ||
| + | |||
| + | with open(' | ||
| + | reader = csv.DictReader(myFile) | ||
| + | myList = [dictionary for dictionary in reader] | ||
| + | |||
| + | # Erste Zeile: | ||
| + | print(myList[0]) | ||
| + | </ | ||
| + | |||
| + | ===== Umwandlung in Zahlen ===== | ||
| + | <code python> | ||
| + | umwandlung = {" | ||
| + | spalte = "Wie geht es dir?" | ||
| + | for zeile in myList: | ||
| + | | ||
| + | </ | ||
| + | |||
| + | Paare zählen | ||
| + | |||
| + | <code python> | ||
| + | tage = 30 | ||
| + | stufen = 5 | ||
| + | anzahl = [[0 for s in range(stufen)] for t in range(tage)] | ||
| + | spalte1 = " | ||
| + | spalte2 = "Wie geht es dir?" | ||
| + | for zeile in myList: | ||
| + | anzahl[zeile[spalte1]][zeile[spalte2]] += 1 | ||
| + | </ | ||
| + | |||
| + | ====== Erstellen der Grafik ====== | ||
| + | Siehe https:// | ||
| + | |||
| + | und https:// | ||
| + | |||
| + | <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, | ||
| + | |||
| + | ax.set(xlim=(0, | ||
| + | | ||
| + | |||
| + | plt.show() | ||
| + | fig.savefig(" | ||
| + | </ | ||