一、Python堆疊柱狀圖顯示標籤
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] data_1 = [10, 20, 30] data_2 = [15, 25, 35] fig, ax = plt.subplots() ax.bar(labels, data_1) ax.bar(labels, data_2, bottom=data_1) ax.legend(labels=['Data 1', 'Data 2']) plt.show()
堆疊柱狀圖是一種用於比較數據變化的可視化方式,它可以讓用戶更加清晰地看到數據的差異和趨勢。在Python中,我們可以使用Matplotlib庫中的bar方法創建堆疊柱狀圖。
在創建堆疊柱狀圖時,有時我們需要為每個柱狀圖添加標籤以便更好地描述數據。可以通過在Matplotlib庫中使用bar方法時,將標籤賦值給labels變量來實現該目的。同時,為了達到堆疊柱狀圖的效果,我們需要在繪製第二組數據時,將其bottom屬性設置為第一組數據的值。
使用legend方法添加一個標籤,用來說明每個柱狀圖所代表的數據系列。
二、Python堆疊柱狀圖Matplotlib
在Python中,我們可以使用Matplotlib庫中的bar方法創建堆疊柱狀圖。Matplotlib是Python中最流行的用於繪製2D圖表和圖形的庫之一,提供了高質量的輸出以及易於使用的接口。
三、Python堆疊柱狀圖代碼
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] data_1 = [10, 20, 30] data_2 = [15, 25, 35] fig, ax = plt.subplots() ax.bar(labels, data_1) ax.bar(labels, data_2, bottom=data_1) ax.legend(labels=['Data 1', 'Data 2']) plt.show()
Python中,使用Matplotlib庫可以方便地繪製堆疊柱狀圖,使用bar方法即可。上面的代碼就是如何使用Matplotlib庫來繪製一個簡單的堆疊柱狀圖的例子。其中,通過傳遞數據和標籤來繪製兩個數據系列的條形圖。
四、Python堆疊柱狀圖group
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] data_1 = [10, 20, 30] data_2 = [15, 25, 35] x = np.arange(len(labels)) width = 0.35 fig, ax = plt.subplots() rects1 = ax.bar(x - width/2, data_1, width, label='Data 1') rects2 = ax.bar(x + width/2, data_2, width, label='Data 2') ax.set_xticks(x) ax.set_xticklabels(labels) ax.legend() plt.show()
在Python中,使用grouped bar plot可以方便地比較多個數據系列中的不同變量。如果要創建一個分組的堆疊柱狀圖,可以使用bar方法和numpy.arange()函數來分組數據。
在上面的代碼中,首先使用numpy.arange()函數來生成每個分組的位置。然後使用width屬性設置每個柱形圖的寬度,並使用rects1和rects2變量引用每個柱形圖的矩形。最後使用ax.legend()方法為圖形添加說明。
五、Python堆疊柱狀圖百分比
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] data_1 = [10, 20, 30] data_2 = [15, 25, 35] fig, ax = plt.subplots() total = [i+j for i,j in zip(data_1, data_2)] data_1_perc = [i / j * 100 for i,j in zip(data_1, total)] data_2_perc = [i / j * 100 for i,j in zip(data_2, total)] ax.bar(labels, data_1_perc) ax.bar(labels, data_2_perc, bottom=data_1_perc) ax.legend(labels=['Data 1', 'Data 2']) plt.show()
如果我們想更好地理解數據的比較和變化,可以考慮將堆疊柱狀圖轉換為百分比的形式。我們可以使用Matplotlib庫的bar方法,並根據數據計算每個類別的百分比。
上述代碼中,我們首先計算每個類別數據的總和,然後利用zip函數計算每個類別數據的百分比。最後,我們使用bar方法和bottom參數繪製兩個數據集的條形圖,並使用legend方法添加標籤。
六、Python堆積代碼柱狀圖
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] data_1 = [10, 20, 30] data_2 = [15, 25, 35] fig, ax = plt.subplots() ax.bar(labels, data_1, label='Data 1') ax.bar(labels, data_2, bottom=data_1, label='Data 2') ax.legend(loc='upper right') plt.show()
堆疊柱狀圖和堆積柱狀圖在視覺效果上很相似,但堆積柱狀圖更加重視單個類別中的變化和比較。
在Python中,我們可以使用Matplotlib庫中的bar方法來創建堆積柱狀圖。與堆疊柱狀圖相同,我們需要為第二個數據系列的bottom參數設置第一個數據系列的值。
七、Python堆疊柱狀圖斜線填充
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [2, 4, 6, 8] y2 = [1, 3, 5, 7] fig, ax = plt.subplots() ax.bar(x, y1, color='g', label='Data 1') ax.bar(x, y2, color='r', bottom=y1, label='Data 2', hatch='/') ax.legend() plt.show()
使用圖案填充堆疊柱狀圖是一種使數據更加易於理解的方法。這可以通過在柱狀圖上應用圖案來實現。
在Python中,可以使用Matplotlib庫中bar方法的hatch參數為柱狀圖添加一個圖案。我們需要為兩個數據集的柱形圖引用不同的顏色,然後在第二個數據集的柱形圖中使用底部參數為其設置基線。
八、Python堆疊柱狀圖標記百分比
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] data_1 = [10, 20, 30] data_2 = [15, 25, 35] fig, ax = plt.subplots() total = [i+j for i,j in zip(data_1, data_2)] data_1_perc = [i / j * 100 for i,j in zip(data_1, total)] data_2_perc = [i / j * 100 for i,j in zip(data_2, total)] ax.bar(labels, data_1_perc) ax.bar(labels, data_2_perc, bottom=data_1_perc) ax.legend(labels=['Data 1', 'Data 2']) for i in range(len(labels)): ax.text(i, data_1_perc[i]/2, f"{data_1_perc[i]:.2f}%", ha='center') ax.text(i, data_1_perc[i]+data_2_perc[i]/2, f"{data_2_perc[i]:.2f}%", ha='center') plt.show()
標註百分比是在堆疊柱狀圖中描述數據變化和趨勢的重要方法。在Python中,我們可以使用Matplotlib庫的text()方法輕鬆地添加標籤。
上述代碼中,我們使用zip函數和循環來遍歷每個數據類別,並利用text()方法將百分比數值添加到每個數據系列的中點位置上。通過設置ha屬性,每個標籤可水平居中對齊。
九、Python堆疊柱狀圖顯示百分比
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] data_1 = [10, 20, 30] data_2 = [15, 25, 35] fig, ax = plt.subplots() total = [i+j for i,j in zip(data_1, data_2)] data_1_perc = [i / j * 100 for i,j in zip(data_1, total)] data_2_perc = [i / j * 100 for i,j in zip(data_2, total)] ax.bar(labels, data_1_perc) ax.bar(labels, data_2_perc, bottom=data_1_perc) ax.legend(labels=['Data 1', 'Data 2']) plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter()) plt.show()
顯示百分比是將數據變化和趨勢可視化的重要方法之一。在Python中,我們可以使用Matplotlib庫中的PercentFormatter類來將y軸刻度轉換為百分比。
在上面的代碼中,我們首先計算每個類別數據的總和,然後利用zip函數計算每個類別數據的百分比。最後,我們使用bar方法和bottom參數繪製兩個數據集的條形圖,並使用legend方法添加標籤。使用PercentFormatter類可以將y軸刻度轉換為百分比格式的字符串。
十、Python堆疊柱狀圖與原始柱狀圖對比
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] data_1 = [10, 20, 30] data_2 = [15, 25, 35] fig, axs = plt.subplots(1, 2, figsize=(10, 5)) axs[0].bar(labels, data_1) axs[0].bar(labels, data_2, bottom=data_1) axs[0].set_title('Stacked Bar Plot') axs[1].bar(labels, data_1) axs[1].bar(labels, data_2, bottom=data_1) axs[1].set_title('Grouped Bar Plot') plt.show()
在的可視化中,使用堆疊柱狀圖和原始柱狀圖之間的選擇,通常由我們需要觀察的數據類型和數據量來決定。
本節中,我們將把堆疊柱狀圖和原始柱狀圖並排放在同一個圖像中進行比較。在圖像上方的標題中,我們使用set_title()方法命名這兩個圖。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/271637.html