python繪制折線圖和條形圖的方法
更新時間:2022年04月21日 09:05:35 作者:檸檬樹下你和我?
這篇文章主要為大家詳細介紹了python實現折線圖和條形圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python繪制折線圖和條形圖的具體代碼,供大家參考,具體內容如下
最近開始寫小論文啦,中間不免要作各種各樣的圖,學習后自己作了個小筆記,供小伙伴一起學習哦。
折線圖
import matplotlib.pyplot as plt
#x軸取值不一樣時
# x1=[0,0.1,0.3,0.5,0.7,0.8,0.9]
# y1=[0.7150,0.7147,0.7088,0.7029,0.6996,0.6942,0.5599]
# x2=[0,0.1,0.2,0.5,0.6,0.8,0.9,1]
# y2=[0.7150,0.7146,0.6969,0.6496,0.5568,0.5196,0.4248,0.3344]
# x3=[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
# y3=[0.7150,0.7147,0.7068,0.7016,0.6283,0.5889,0.5155,0.4992,0.4728,0.3909,0.3310]
# x軸取值一樣時
x = [1,2,3,4,5,6,7,8,9,10,11,12]
y1 = [57,74,66,69,88,82,78,70,80,92,69,99]
y2 = [44,47,48,55,56,48,86,69,58,60,63,79]
y3 = [61,77,59,85,79,80,53,48,50,66,88,81]
plt.title('快遞月件量') ?# 折線圖標題
plt.rcParams['font.sans-serif'] = ['SimHei'] ?# 折線圖中需顯示漢字時,得加上這一行
plt.xlabel('月份/月') ?# x軸標題
plt.ylabel('快遞件數') ?# y軸標題
plt.plot(x, y1, marker='o', markersize=3) ?# 繪制折線圖,添加數據點形狀并設置點的大小
plt.plot(x, y2, marker='^', markersize=3) ?#^:點的形狀為三角形
plt.plot(x, y3, marker='*', markersize=3) ?#星形
for a, b in zip(x, y1):
? ? plt.text(a, b, b, ha='center', va='bottom', fontsize=10) ?# 設置數據標簽位置及字體大小
for a, b in zip(x, y2):
? ? plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
for a, b in zip(x, y3):
? ? plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
plt.legend(['郵政', '順豐', '圓通']) ?# 設置折線名稱
plt.show() ?# 顯示折線圖結果:

單條形圖
import matplotlib.pyplot as plt
# 條形圖需要顯示中文時,需要下面這兩行代碼
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
quarters = ('第一節(jié)度', '第二季度', '第三季度', '第四季度') ?#x軸
courier_number = [310, 382, 256, 402] ?#x軸對應的數量
plt.bar(quarters, courier_number) ? #作圖
#plt.barh(quarters, courier_number) ?# 若要橫放條形圖,用函數barh
plt.title('四個季度快遞數量的調查結果') ?#條形圖標題
plt.show()結果:

并列條形圖
import matplotlib.pyplot as plt
import numpy as np
# 條形圖需要顯示中文時,需要下面這兩行代碼
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 輸入統(tǒng)計數據
quarters = ('第一節(jié)度', '第二季度', '第三季度', '第四季度') ?#x軸
courier_number_before = [310, 382, 256, 402]
courier_number_now = [320, 420, 388, 432]
bar_width = 0.3 ?# 設置條形寬度
index_before = np.arange(len(quarters)) ?# 之前四季度條形圖的橫坐標
index_now = index_before + bar_width ?# 現在四季度條形圖的橫坐標
# 使用兩次 bar 函數畫出兩組條形圖
plt.bar(index_before, height=courier_number_before, width=bar_width, color='b', label='去年')
plt.bar(index_now, height=courier_number_now, width=bar_width, color='g', label='今年')
plt.legend() ?# 顯示圖例
plt.xticks(index_before + bar_width/2, quarters) ?# 讓橫坐標軸刻度顯示 四個季度的快遞量, index_before + bar_width/2 為橫坐標軸刻度的位置
plt.ylabel('快遞數量') ?# 縱坐標軸標題
plt.title('去年今年四個季度快遞數量的調查結果') ?# 圖形標題
plt.show()
大家根據需要進行代碼相應的改變。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python文本情感分類識別基于SVM算法Django框架實現
這篇文章主要為大家介紹了Python文本情感分類識別基于SVM算法Django框架實現詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
Python圖像處理實現兩幅圖像合成一幅圖像的方法【測試可用】
這篇文章主要介紹了Python圖像處理實現兩幅圖像合成一幅圖像的方法,結合實例形式分析了Python使用Image.blend()接口與Image.composite()接口進行圖像合成的相關操作技巧,需要的朋友可以參考下2019-01-01

