Python——使用matplotlib繪制柱狀圖
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),札達(dá)企業(yè)網(wǎng)站建設(shè),札達(dá)品牌網(wǎng)站建設(shè),網(wǎng)站定制,札達(dá)網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,札達(dá)網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
1、基本柱狀圖
首先要安裝matplotlib 可以使用pip命令直接安裝
[python]?view plain?copy
#?-*-?coding:?utf-8?-*-
import?matplotlib.pyplot?as?plt
num_list?=?[1.5,0.6,7.8,6]
plt.bar(range(len(num_list)),?num_list)
plt.show()
2、設(shè)置顏色
[python]?view plain?copy
#?-*-?coding:?utf-8?-*-
import?matplotlib.pyplot?as?plt
num_list?=?[1.5,0.6,7.8,6]
plt.bar(range(len(num_list)),?num_list,fc='r')
plt.show()
[cpp]?view plain?copy
#?-*-?coding:?utf-8?-*-
import?matplotlib.pyplot?as?plt
num_list?=?[1.5,0.6,7.8,6]
plt.bar(range(len(num_list)),?num_list,color='rgb')
plt.show()
3、設(shè)置標(biāo)簽
[python]?view plain?copy
#?-*-?coding:?utf-8?-*-
import?matplotlib.pyplot?as?plt
name_list?=?['Monday','Tuesday','Friday','Sunday']
num_list?=?[1.5,0.6,7.8,6]
plt.bar(range(len(num_list)),?num_list,color='rgb',tick_label=name_list)
plt.show()
4、堆疊柱狀圖
matlab實(shí)現(xiàn)演示效果如下:
%需要新建一個(gè)function,以下是function的代碼(保存時(shí)文件名只能是rotateticklabel.m):
function th=rotateticklabel(h,rot,demo)
%ROTATETICKLABEL rotates tick labels
% ? TH=ROTATETICKLABEL(H,ROT) ris the calling form where H is a handle to
% ? the axis that contains the XTickLabels that are to be rotated. ROT is
% ? an optional parameter that specifies the angle of rotation. The default
% ? angle is 90. TH is a handle to the text objects created. For long
% ? strings such as those produced by datetick, you may have to adjust the
% ? position of the axes so the labels don't get cut off.
%
% ? Of course, GCA can be substituted for H if desired.
%
% ? TH=ROTATETICKLABEL([],[],'demo') shows a demo figure.
%
% ? Known deficiencies: if tick labels are raised to a power, the power
% ? will be lost after rotation.
%
% ? See also datetick.
% ? Written Oct 14, 2005 by Andy Bliss
% ? Copyright 2005 by Andy Bliss
%DEMO:
if nargin==3
x=[now-.7 now-.3 now];
y=[20 35 15];
figure
plot(x,y,'.-')
datetick('x',0,'keepticks')
h=gca;
set(h,'position',[0.13 0.35 0.775 0.55])
rot=90;
end
%set the default rotation if user doesn't specify
if nargin==1
rot=90;
end
%make sure the rotation is in the range
% 0:360 (brute force method)
% while rot360
% ? ? rot=rot-360;
% end
% while rot0
% ? ? rot=rot+360;
% end
%get current tick labels
a=get(h,'XTickLabel');
%erase current tick labels from figure
set(h,'XTickLabel',[]);
%get tick label positions
b=get(h,'XTick');
c=get(h,'YTick');
%make new tick labels
if rot180
th=text(b,repmat(c(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','right','fontsize',14,'fontweight','bold','rotation',rot);
else
th=text(b,repmat(c(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','left','fontsize',14,'fontweight','bold','rotation',rot);
end
%畫好圖需要旋轉(zhuǎn)坐標(biāo)時(shí)調(diào)用上面的rotateticklabel函數(shù),比如用以下的測(cè)試數(shù)據(jù)
x = round(rand(5,3)*10);
h=bar(x,1,'group');
set(gca,'xticklabels',{'benchmark1','benchmark2','benchmark3','benchmark4','benchmark5'});
h = gca;
th=rotateticklabel(h, 45)
%滿意請(qǐng)采納
首先,dataframe自帶的柱狀圖,可以將每列作為一個(gè)圖例
import pandas as pd
data=pd.read_excel()
data.bar()