Zweite Y-Achse in Plot aus CSV-Datei

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
Kahnbein.Kai
User
Beiträge: 104
Registriert: Mittwoch 24. Juni 2015, 14:12
Wohnort: Bochum

Guten Abend,
ich möchte eine zweite Y-Achse in mein Plot einfügen. Bisher habe ich beide Datenlinien auf einer Y-Achse dargestellt. Das ist mein Code.
In den Codefenster habe ich code=Python angegeben, in der Vorschu funktioniert die Einfärbung nicht, warum weiß nicht nicht.

Code: Alles auswählen

import matplotlib.pyplot as plt
import pandas as pd

Tab = pd.read_csv('Mst01.csv', delimiter=';')
 
#pd.set_option("display.max_columns",999) Setzt die columns Anzahl auf 999

print(Tab.head(5)) # Zeigt den Head mit 5 Zeilen an
print(list(Tab))

print(Tab.info())

ax = plt.gca()

Tab.plot(kind='line',x='Nr. ',y='cm', label='cm', ax=ax)
Tab.plot(kind='line',x='Nr. ',y='l/s', color='red', ax=ax)

plt.xlabel('Nr.')
plt.ylabel('cm')
plt.title('Mst01')
plt.legend()

plt.show()


Hier ist die Matplotlib Docu https://matplotlib.org/gallery/api/two_scales.html, dort wird gezeigt wie man zwei y-Achsen erstellt. Allerdings sind dort data1 und data2 nicht aus einer CSV File.
Muss ich die CSV-Datei als Array einlesen z. B. data1 = Nr.; Data2 = cm u.s.w. ?

Da stehe ich gerade auf dem Schlauch.
Bild
Gruß Kai
Kahnbein.Kai
User
Beiträge: 104
Registriert: Mittwoch 24. Juni 2015, 14:12
Wohnort: Bochum

Ich habe es geschafft !!

Code: Alles auswählen

import pandas as pd
import matplotlib.pyplot as plt

Tab = pd.read_csv('Mst01.csv', delimiter=';')

x =  Tab['Nr. ']
y1 = Tab['cm']
y2 = Tab['l/s']

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')

ax1.set_xlabel('Nr.')
ax1.set_ylabel('cm', color='g')

ax2.set_ylabel('l/s', color='b')


plt.title('Mst01')

plt.legend()

plt.show()
Antworten