何がこの積み重ね棒グラフで間違って起こっていますか?

SHV_la:

私は本当にこれで間違って何が起こっているのか理解していない...私はかなり単純なデータが何であるかを何度も見てきたし、カーネル(Jupyterノートブック上で実行されている)を再起動し、何もそれを解決しているように見えるしていません。

ここで私が持っているデータフレームです(申し訳ありませんが、私は数字は少し愚かに見える知っているが、これはオリジナルの20年間のインデックスを再作成され、長期間にわたり本当にまばらなデータセットです):

DATE        NODP            NVP             VP              VDP
03/08/2002  0.083623        0.10400659      0.81235517      1.52458E-05
14/09/2003  0.24669167      0.24806379      0.5052293       1.52458E-05
26/07/2005  0.15553726      0.13324796      0.7111538       0.000060983
20/05/2006  0               0.23            0.315           0.455
05/06/2007  0.21280034      0.29139224      0.49579217      1.52458E-05
21/02/2010  0               0.55502195      0.4449628       1.52458E-05
09/04/2011  0.09531311      0.17514162      0.72954527      0
14/02/2012  0.19213217      0.12866237      0.67920546      0
17/01/2014  0.12438848      0.10297326      0.77263826      0
24/02/2017  0.01541347      0.09897548      0.88561105      0

すべての行が1まで追加ことに注意してください!私は、トリプル、四重はこの... XDをチェックしています

私は、私はそれを使用している他のすべてのために完璧に働いているようだ次のコードで、このデータの積み上げ棒グラフを作成しようとしています:

NODP = df['NODP']
NVP = df['NVP']
VDP = df['VDP']
VP = df['VP']
ind = np.arange(len(df.index))
width = 5.0

p1 = plt.bar(ind, NODP, width, label = 'NODP', bottom=NVP, color= 'grey')
p2 = plt.bar(ind, NVP, width, label = 'NVP', bottom=VDP, color= 'tan')
p3 = plt.bar(ind, VDP, width, label = 'VDP', bottom=VP, color= 'darkorange')
p4 = plt.bar(ind, VP, width, label = 'VP', color= 'darkgreen')
plt.ylabel('Ratio')
plt.xlabel('Year')
plt.title('Ratio change',x=0.06,y=0.8)
plt.xticks(np.arange(min(ind), max(ind)+1, 6.0), labels=xlabels) #the xticks were cumbersome so not included in this example code
plt.legend()

これは私に次のプロットを与えます:

ここでは、画像の説明を入力します。

明らかなように、1)NODPは全く現れて、及び2)それらの残りは間違った割合でプロットされているれていません...

私は本当に正しい、それは本当に簡単でなければなりません、間違っているものを理解していません!私はそれが本当に簡単であれば、それは右の私の鼻の下で、おそらくですごめんなさい。任意のアイデアも大歓迎します!

JohanC:

あなたはすべての下のバーの合計になるように、この方法(プロットするためにパンダやseabornを使用しないので、標準matplotlibの)下のニーズを重ねバーを作成したい場合。

ここで与えられたデータとの一例です。

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

columns = ['DATE', 'NODP', 'NVP', 'VP', 'VDP']
data = [['03/08/2002', 0.083623, 0.10400659, 0.81235517, 1.52458E-05],
        ['14/09/2003', 0.24669167, 0.24806379, 0.5052293, 1.52458E-05],
        ['26/07/2005', 0.15553726, 0.13324796, 0.7111538, 0.000060983],
        ['20/05/2006', 0, 0.23, 0.315, 0.455],
        ['05/06/2007', 0.21280034, 0.29139224, 0.49579217, 1.52458E-05],
        ['21/02/2010', 0, 0.55502195, 0.4449628, 1.52458E-05],
        ['09/04/2011', 0.09531311, 0.17514162, 0.72954527, 0],
        ['14/02/2012', 0.19213217, 0.12866237, 0.67920546, 0],
        ['17/01/2014', 0.12438848, 0.10297326, 0.77263826, 0],
        ['24/02/2017', 0.01541347, 0.09897548, 0.88561105, 0]]
df = pd.DataFrame(data=data, columns=columns)
ind = pd.to_datetime(df.DATE)
NODP = df.NODP.to_numpy()
NVP = df.NVP.to_numpy()
VP = df.VP.to_numpy()
VDP = df.VDP.to_numpy()

width = 120
p1 = plt.bar(ind, NODP, width, label='NODP', bottom=NVP+VDP+VP, color='grey')
p2 = plt.bar(ind, NVP, width, label='NVP', bottom=VDP+VP, color='tan')
p3 = plt.bar(ind, VDP, width, label='VDP', bottom=VP, color='darkorange')
p4 = plt.bar(ind, VP, width, label='VP', color='darkgreen')
plt.ylabel('Ratio')
plt.xlabel('Year')
plt.title('Ratio change')
plt.yticks(np.arange(0, 1.001, 0.1))
plt.legend()
plt.show()

結果のプロット

この場合、xは軸日で測定し、各バーはその日に配置されていることに留意されたいです。これが重要である場合にはこれは、日付の間の相対的な間隔を知るのに役立ちます。それが重要でない場合は、X-位置は等距離で選ばれ、日付欄を経由して標識することができます。

標準matplotlibのでこれを行うには、次のコードが変更になります。

ind = range(len(df))
width = 0.8
plt.xticks(ind, df.DATE, rotation=20)
plt.tight_layout() # needed to show the full labels of the x-axis

例えば、プロット

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=377282&siteId=1