教育行業(yè)A股IPO第一股(股票代碼 003032)

全國(guó)咨詢/投訴熱線:400-618-4000

Python培訓(xùn)實(shí)例:標(biāo)記不同產(chǎn)品各季度的銷售額

更新時(shí)間:2022年03月30日11時(shí)06分 來源:傳智教育 瀏覽次數(shù):

  已知某公司旗下共有3款明星產(chǎn)品:產(chǎn)品A、產(chǎn)品B和產(chǎn)品C。為了解每款產(chǎn)品全年的銷售額,公司對(duì)每款產(chǎn)品的年銷售額進(jìn)行了核算,核算之后的結(jié)果如表4-5所示。

  表4-5 不同產(chǎn)品各季度的銷售額

不同產(chǎn)品各季度銷售額

  根據(jù)表4-5的數(shù)據(jù),將“季度”一列的數(shù)據(jù)作為x軸的刻度標(biāo)簽,將“產(chǎn)品A”“產(chǎn)品B”“產(chǎn)品C”三列的數(shù)據(jù)作為y軸的數(shù)據(jù),分別使用plot()函數(shù)繪制反映產(chǎn)品A、產(chǎn)品B和產(chǎn)品C各季度銷售額的折線圖,并使用不同的線型、顏色、標(biāo)記進(jìn)行區(qū)分,具體代碼如下。

# 03_sales_of_different_products
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
sale_a = [2144, 4617, 7674, 6666]
sale_b = [853, 1214, 2414, 4409]
sale_c = [153, 155, 292, 680]
fig = plt.figure()
ax = fig.add_subplot(111)
# 繪制具有不同線條樣式的折線圖
ax.plot(sale_a, 'D-', sale_b, '^:', sale_c, 's--')
ax.grid(alpha=0.3)
ax.set_ylabel('銷售額(萬元)')
ax.set_xticks(np.arange(len(sale_c)))
ax.set_xticklabels(['第一季度', '第二季度', '第3季度', '第4季度'])
ax.legend(['產(chǎn)品A', '產(chǎn)品B', '產(chǎn)品C'])
plt.show()

  運(yùn)行程序,效果如圖4-7所示。

  圖4-7中,每條折線均使用不同樣式的數(shù)據(jù)標(biāo)記標(biāo)注了數(shù)據(jù)的位置,其中藍(lán)色折線使用菱形標(biāo)注了產(chǎn)品A各季度的銷售額;橙色折線使用正三角形標(biāo)注了產(chǎn)品B各季度的銷售額;綠色折線使用正方形標(biāo)注了產(chǎn)品C各季度的銷售額。由圖4-7可知,產(chǎn)品A在各季度的銷售額都高于另兩個(gè)產(chǎn)品,產(chǎn)品C在各季度的銷售額都低于另兩個(gè)產(chǎn)品。

不同產(chǎn)品各季度銷售額折線圖

  圖4-7 不同產(chǎn)品各季度的銷售額的折線圖

0 分享到:
和我們?cè)诰€交談!