簡體   English   中英

如何在matplotlib中分隔堆積的條形圖?

[英]How to seperate stacked bars in matplotlib?

我有兩本字典,其價值是人數...

d_1={1947: 1, 1950: 1, 1951: 2, 1955: 2, 1956: 1, 1957: 2, 1958: 2, 1959: 3, 1960: 1....}

d_2={1936: 1, 1945: 1, 1948: 2, 1949: 1, 1950: 2, 1951: 2, 1952: 3, 1953: 1, 1954: 41..}

我想創建顯示人數的條形圖。 X軸在year形成。

當我使用此代碼格式時,除堆疊的條形外幾乎相同。 我希望將它們分開。如何解決?

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt; plt.rcdefaults()

    x1=[t for t in d_1.keys()]
    y1=[k for k in d_1.values()]
    x2=[m for m in d_2.keys()]
    y2=[n for n in d_2.values()]


    fig, ax = plt.subplots()

    index = np.arange(len(years_without_duplicates)) 
    #list which has the years without duplicates

    bar_width = 0.35

    rects1 = plt.bar(x1, y1, bar_width,color='b')
    rects2 = plt.bar(x2, y2, bar_width, color='r')

    plt.xlabel('Years')
    plt.ylabel('number of people')
    plt.xticks([i for i in range(min(years_without_duplicates),max(years_without_duplicates)+1)],rotation=80,fontsize=6)

    plt.legend()



    plt.tight_layout()

    plt.show()

我的酒吧堆砌了,但我希望將它們分開。

只需移動酒吧:

bar_width = 0.35
x1 = [t - bar_width / 2 for t in d_1.keys()]
y1 = [k for k in d_1.values()]
x2 = [m + bar_width / 2 for m in d_2.keys()]
y2 = [n for n in d_2.values()]

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM