Python利用pandas和matplotlib實現(xiàn)繪制堆疊柱狀圖
在數(shù)據(jù)可視化中,堆疊柱狀圖是一種常用的圖表類型,它能夠清晰地展示多個類別的數(shù)據(jù),并突出顯示每個類別中各部分的總量和組成比例。本文將演示如何使用 Python 的 pandas 和 matplotlib 庫繪制優(yōu)化的堆疊柱狀圖,并展示了銷售數(shù)量隨店鋪名稱變化的情況。
導入必要的庫
首先,我們需要導入 pandas 和 matplotlib.pyplot 庫,并指定中文字體為黑體,代碼如下:
import pandas as pd import matplotlib.pyplot as plt plt.rcParams['font.family'] = ['SimHei'] # 指定中文字體為黑體
讀取數(shù)據(jù)
接下來,我們使用 pandas 的 read_excel
函數(shù)讀取 Excel 文件中的數(shù)據(jù),并指定讀取的工作表名稱為“Sheet3”,如下所示:
df = pd.read_excel(r'C:\Users\liuchunlin2\Desktop\新建文件夾\新建 XLSX 工作表.xlsx', sheet_name='Sheet3')
設置圖形參數(shù)
在繪制堆疊柱狀圖之前,我們需要設置柱狀圖的寬度和 x 軸的位置,代碼如下:
bar_width = 0.35 # 設置柱狀圖的寬度 x = df.index # 設置x軸的位置
繪制堆疊柱狀圖
使用 matplotlib 庫的 subplots 函數(shù)創(chuàng)建圖形對象,并使用 bar 函數(shù)繪制堆疊柱狀圖,具體代碼如下:
fig, ax = plt.subplots() rects1 = ax.bar(x, df['銷售數(shù)量'], bar_width, label='銷售數(shù)量') rects2 = ax.bar(x, df['銷售數(shù)量2'], bar_width, bottom=df['銷售數(shù)量'], label='銷售數(shù)量2')
添加標簽和標題
我們?yōu)閳D形添加軸標簽、標題、刻度和圖例,使其更具可讀性,具體代碼如下:
ax.set_xlabel('店鋪名稱') ax.set_ylabel('銷售數(shù)量') ax.set_title('Stacked Bar Chart') ax.set_xticks(x) ax.set_xticklabels(df['店鋪名稱']) ax.legend()
顯示數(shù)據(jù)標簽
最后,我們使用 annotate 函數(shù)在每個柱子上方顯示數(shù)據(jù)標簽,以展示具體的銷售數(shù)量,具體代碼如下:
for rect in rects1: height = rect.get_height() ax.annotate(f'{height}', xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), textcoords='offset points', ha='center', va='bottom') for rect1, rect2 in zip(rects1, rects2): height1 = rect1.get_height() height2 = rect2.get_height() total_height = height1 + height2 ax.annotate(f'{height2}', xy=(rect2.get_x() + rect2.get_width() / 2, total_height), xytext=(0, 3), textcoords='offset points', ha='center', va='bottom')
顯示圖形
最后,使用 plt.show()
函數(shù)顯示繪制好的堆疊柱狀圖,代碼如下:
plt.show()
通過以上步驟,我們成功繪制出了堆疊柱狀圖,展示了不同店鋪的銷售數(shù)量情況。
圖表效果圖展示
完整代碼
import pandas as pd import matplotlib.pyplot as plt plt.rcParams['font.family'] = ['SimHei'] # 指定中文字體為黑體 # 讀取Excel文件 df = pd.read_excel(r'C:\Users\liuchunlin2\Desktop\新建文件夾\新建 XLSX 工作表.xlsx', sheet_name='Sheet3') # 設置柱狀圖的寬度 bar_width = 0.35 # 設置x軸的位置 x = df.index # 繪制堆疊柱狀圖 fig, ax = plt.subplots() rects1 = ax.bar(x, df['銷售數(shù)量'], bar_width, label='銷售數(shù)量') rects2 = ax.bar(x, df['銷售數(shù)量2'], bar_width, bottom=df['銷售數(shù)量'], label='銷售數(shù)量2') # 添加標簽和標題 ax.set_xlabel('店鋪名稱') ax.set_ylabel('銷售數(shù)量') ax.set_title('Stacked Bar Chart') ax.set_xticks(x) ax.set_xticklabels(df['店鋪名稱']) ax.legend() # 在每個柱子上方顯示數(shù)據(jù)標簽 for rect in rects1: height = rect.get_height() ax.annotate(f'{height}', xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), textcoords='offset points', ha='center', va='bottom') for rect1, rect2 in zip(rects1, rects2): height1 = rect1.get_height() height2 = rect2.get_height() total_height = height1 + height2 ax.annotate(f'{height2}', xy=(rect2.get_x() + rect2.get_width() / 2, total_height), xytext=(0, 3), textcoords='offset points', ha='center', va='bottom') # 顯示圖形 plt.show()
到此這篇關于Python利用pandas和matplotlib實現(xiàn)繪制堆疊柱狀圖的文章就介紹到這了,更多相關Python堆疊柱狀圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)讀取文件夾按數(shù)字排序功能
這篇文章主要介紹了Python讀取文件夾按數(shù)字排序,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09Python使用thread模塊實現(xiàn)多線程的操作
線程(Threads)是操作系統(tǒng)提供的一種輕量級的執(zhí)行單元,可以在一個進程內(nèi)并發(fā)執(zhí)行多個任務,每個線程都有自己的執(zhí)行上下文,包括棧、寄存器和程序計數(shù)器,本文給大家介紹了Python使用thread模塊實現(xiàn)多線程的操作,需要的朋友可以參考下2024-10-10Python Pytorch深度學習之數(shù)據(jù)加載和處理
今天小編就為大家分享一篇Pytorch 數(shù)據(jù)加載與數(shù)據(jù)預處理方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-10-10