Seite 1 von 1

bar verhält sich unregelmäßig

Verfasst: Dienstag 17. August 2021, 13:31
von Brando
Folgender Code funktioniert nicht:

Code: Alles auswählen

%matplotlib notebook
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(12345)

df = pd.DataFrame([np.random.normal(32000,200000,3650), 
                   np.random.normal(43000,100000,3650), 
                   np.random.normal(43500,140000,3650), 
                   np.random.normal(48000,70000,3650)], 
                  index=[1992,1993,1994,1995])
# print (df)
bottom_1_min = df.loc[int("1992")].min()
bottom_1_max = df.loc[int("1992")].max()
diff_1 = bottom_1_max - bottom_1_min
bottom_2_min = df.loc[int("1993")].min()
bottom_2_max = df.loc[int("1993")].max()
diff_2 = bottom_2_max - bottom_2_min
year = ["1992", "1993"]
diff = [diff_1, diff_2]
if isinstance(diff_1, np.generic):
    print (diff_1)
plt.bar(year, diff)
plt.show()
Die Fehlermeldung ist, dass plt.bar nicht funktioniert: unsupported operand type(s) for -: 'str' and 'float'
Jedes Tutorial erzählt mir, dass mein Code funktionieren müsste! Was ist falsch?

Re: bar verhält sich unregelmäßig

Verfasst: Dienstag 17. August 2021, 13:37
von __deets__
Ich weiß nicht welches Tutorial du meinst. Aber was ist der Unterschied zwischen 1234 und “1234” und int(“1234”)?

Re: bar verhält sich unregelmäßig

Verfasst: Dienstag 17. August 2021, 13:40
von Sirius3
Dein Code tut ja auch. Jedenfalls der, den Du hier gepostet hast.
Wie lautet denn der komplette Traceback.

Re: bar verhält sich unregelmäßig

Verfasst: Dienstag 17. August 2021, 13:51
von rogerb
@Brando,

bei mir funktioniert es ohne Probleme. Schwer zusagen, was bei dir anders ist.

Ich verwende:
matplotlib==3.3.4
pandas==1.2.5
python 3.9

Re: bar verhält sich unregelmäßig

Verfasst: Dienstag 17. August 2021, 16:25
von Brando
Weiß nicht welche Version ich habe! Aber ich musste folgendes machen, damit das DATUM in der x Achse erscheint:

Code: Alles auswählen

fig, ax = plt.subplots()
#create chart
ax.bar( OfCall, #x-coordinates of bars
       height= mean_b, #height of bars
       yerr= l_sem, #error bar width
       capsize=4) #length of error bar caps
ax.set_xticks(OfCall)
ax.set_xticklabels(['1992', "1993"])