Seite 1 von 1

Mehrere Dataframes plotten

Verfasst: Mittwoch 3. Februar 2021, 13:47
von tmessers
Hallo zuammen,

ich wende mich mit einem Problem an Euch, woran ich gerade hänge.
Ich möchte lediglich 2 Dataframes in einer figure darstellen.

Der Code sieht so aus

Code: Alles auswählen

def bw()
    ...
    return (df_graf_bw)

def fra()
    .... 
    return (df_graf)

def plotten(df_graf_bw, df_graf):
    print ("Plotten beginnen")
    print (daten_1)
    print (daten_2)

    fig, axes = plt.subplots(nrows=2, ncols=1)
    df_graf_bw.plot(ax=axes[0,0])
    df_graf.plot(ax=axes[0,1])

    plt.show
    print ("Plotten Ende")

if __name__ == "__main__":
    daten_1 = bw()
    daten_2 = fra()
    plotten(daten_1,daten_2)
Das Ergebnis ist
Plotten beginnen
Baden-Baden Rastatt Freudenstadt Freiburg Ortenaukreis Rottweil
27.01. 29.0 43.2 63.4 55.8 120.2 88.6
28.01. 18.1 41.1 64.3 53.2 114.2 85.1
29.01. 19.9 45.8 55.8 42.4 117.4 66.5
30.01. 19.9 45.8 55.8 42.4 117.4 66.5
31.01. 19.9 45.8 55.8 42.4 117.4 66.5
01.02. 29.0 42.3 49.1 44.1 126.2 92.2
03.02. 36.2 43.6 33.8 41.1 121.6 75.8
France Bas-Rhin Haut-Rhin Strasbourg IdF
24.01. 200 199 185 226 0
25.01. 208 218 206 239 0
26.01. 210 219 221 240 0
27.01. 210 218 219 240 214
28.01. 211 224 209 248 214
29.01. 211 224 208 247 214
02.02. 212 225 195 271 214
Traceback (most recent call last):
File "/Users/ich/Python/corona/corona_neu.py", line 525, in <module>
plotten(daten_1,daten_2)
File "/Users/ich/Python/corona/corona_neu.py", line 480, in plotten
df_graf_bw.plot(ax=axes[0,0])
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

Ich habe verschiedene Lösungen aus diversen Foren ausprobiert, finde aber keine passende Lösung.
Weiß wer Rat?

Danke

Re: Mehrere Dataframes plotten

Verfasst: Mittwoch 3. Februar 2021, 14:04
von __blackjack__
Deutlicher als die Fehlermeldung kann man ja kaum werden. `axes` ist halt nur 1-Dimensional und da macht ein 2D-Index keinen Sinn.

Code: Alles auswählen

In [461]: import matplotlib.pyplot as plt                                       

In [462]: fig, axes = plt.subplots(nrows=2, ncols=1)                            

In [463]: axes                                                                  
Out[463]: 
array([<matplotlib.axes._subplots.AxesSubplot object at 0x7fb934c36d68>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x7fb934c524a8>],
      dtype=object)

In [464]: axes.shape                                                            
Out[464]: (2,)

In [465]: axes[0, 0]                                                            
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-465-eda7c1ec264b> in <module>
----> 1 axes[0, 0]

IndexError: too many indices for array

In [466]: axes[0]                                                               
Out[466]: <matplotlib.axes._subplots.AxesSubplot at 0x7fb934c36d68>

In [467]: axes[1]                                                               
Out[467]: <matplotlib.axes._subplots.AxesSubplot at 0x7fb934c524a8>