python畫條形圖實(shí)例
本文實(shí)例為大家分享了python畫條形圖的具體代碼,供大家參考,具體內(nèi)容如下
在做畢設(shè)的過(guò)程中有些數(shù)據(jù)用表格來(lái)展現(xiàn),會(huì)很難看出數(shù)據(jù)之間的差別,凸顯不出數(shù)據(jù)的特點(diǎn),所以想制作一個(gè)條形圖,這里特地記錄下,已備以后用到。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
#指定默認(rèn)字體
matplotlib.rcParams['font.sans-serif'] = ['FangSong']
matplotlib.rcParams['font.family']='sans-serif'
?
c101= (8,7,3,7,10,4,11,8,11,10,8,9)
c102= (21,21,24,25,16,17,11,24,24,25,36,33)
c103= (4,4,10,3,9,8,12,2,4,5,2,6)
c104= (1,5,1,3,2,3,3,3,6,5,1,0)
c105= (3,1,0,1,3,3,1,2,0,0,0,0)
c106= (1,2,0,0,1,1,0,0,1,1,0,0)
c107= (1,0,1,0,0,1,0,0,0,0,0,0)
c108= (0,1,0,0,0,2,1,1,0,1,0,0)
c109= (1,0,1,1,0,0,1,1,0,0,0,0)
?
ind = np.arange(0,24,2) # the x locations for the groups
width = 0.2 ?# the width of the bars
?
fig,ax = plt.subplots()
rects1 = ax.bar(ind + width, c101, width, color='SkyBlue',align='edge', label='101')
rects2 = ax.bar(ind + 2*width, c102, width,color='IndianRed',align='edge', label='102')
rects3 = ax.bar(ind + 3*width, c103, width, color='Cyan',align='edge', label='103')
rects4 = ax.bar(ind + 4*width, c104, width, color='Magenta',align='edge', label='104')
rects5 = ax.bar(ind + 5*width, c105, width, color='Purple',align='edge', label='105')
rects6 = ax.bar(ind + 6*width, c106, width, color='Green',align='edge', label='106')
rects7 = ax.bar(ind + 7*width, c107, width, color='Yellow',align='edge', label='107')
rects8 = ax.bar(ind + 8*width, c108, width, color='Blue',align='edge', label='108')
rects9 = ax.bar(ind + 9*width, c109, width, color='Orange',align='edge', label='109')
?
# Add some text for labels, title and custom x-axis tick labels, etc.
#ax.set_title('Scores by group and gender')
plt.xticks(ind,('1班', '2班', '3班', '4班', '5班','6班','7班','8班','9班','10班','11班','12班'))
ax.legend(loc='upper center')
?
plt.show()
fig.savefig('./test77.jpg')
豎起來(lái)的
%matplotlib notebook
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
#指定默認(rèn)字體
matplotlib.rcParams['font.sans-serif'] = ['FangSong']
matplotlib.rcParams['font.family']='sans-serif'
#解決負(fù)號(hào)'-'顯示為方塊的問(wèn)題
matplotlib.rcParams['axes.unicode_minus'] = False
data = [[8, 21, 4, 1, 3, 1, 1, 0, 1],
? ? ? ? [7,21,4,5,1,2,0,1,0],?
? ? ? ? [3,24,10,1,0,0,1,0,1],
? ? ? ? [7,25,3,3,1,0,0,0,1],?
? ? ? ? [10,16,9,2,3,1,0,0,0],?
? ? ? ? [4,17,8,3,3,1,1,2,0],?
? ? ? ? [11,11,12,3,1,0,0,1,1],?
? ? ? ? [8,24,2,3,2,0,0,1,1],?
? ? ? ? [11,24,4,6,0,1,0,0,0],?
? ? ? ? [10,25,5,5,0,1,0,1,0],?
? ? ? ? [8,36,2,1,0,0,0,0],?
? ? ? ? [9,33,6,0,0,0,0,0]]
df = pd.DataFrame(data,
? ? ? ? ? ? ? ? ?index=['1班','2班','3班','4班','5班','6班','7班','8班','9班','10班','11班','12班'],
? ? ? ? ? ? ? ? ?columns=pd.Index(['101','102','103','104','105','106','107','108','109']),
? ? ? ? ? ? ? ? ?)
df.plot(kind='barh',figsize=(5,8))?
plt.show()
#fig.savefig('./test2.jpg')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python中DJANGO簡(jiǎn)單測(cè)試實(shí)例
這篇文章主要介紹了Python中DJANGO簡(jiǎn)單測(cè)試,實(shí)例分析了DJANGO的用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05
OpenCV特征提取與檢測(cè)之Harris角點(diǎn)檢測(cè)
這篇文章主要給大家介紹了關(guān)于OpenCV特征提取與檢測(cè)之Harris角點(diǎn)檢測(cè)的相關(guān)資料,Harris角點(diǎn)檢測(cè)的目的是去分辨出圖像中的平面、邊界以及角點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
python實(shí)現(xiàn)讀取Excel內(nèi)容并展示成json
這篇文章主要為大家詳細(xì)介紹了如何使用python實(shí)現(xiàn)讀取Excel內(nèi)容并展示成json功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-12-12
Python實(shí)現(xiàn)繪制Matlab格式的地圖邊框的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)繪制Matlab格式的地圖邊框,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-09-09
python實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)包簽名
這篇文章主要介紹了python實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)包簽名,主要以python怎么快速對(duì)請(qǐng)求體做一次簽名為主題,塑造實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)包簽名過(guò)程,具有一定得參考價(jià)值,需要的小伙伴可以參考一下2022-02-02
對(duì)django 模型 unique together的示例講解
今天小編就為大家分享一篇對(duì)django 模型 unique together的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08

