python繪制餅圖和直方圖的方法
本文實(shí)例為大家分享了python繪制餅圖和直方圖的具體代碼,供大家參考,具體內(nèi)容如下


#餅圖,常與結(jié)構(gòu)分析結(jié)合使用
import pandas
import numpy
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
#導(dǎo)入數(shù)據(jù)
plot_pie=pandas.read_csv('D://Python projects//reference data//6.3//data.csv')
#計(jì)算每個(gè)品牌的用戶數(shù),保留序列
result=plot_pie.groupby(
? ? ? ? by=['通信品牌'],
? ? ? ? as_index=False
? ? ? ? )['號(hào)碼'].agg({
? ? ? ? ? ? ? ? '用戶數(shù)':numpy.size})
#使用彈窗繪圖
%matplotlib qt
#設(shè)置長(zhǎng)寬分辨率
plt.figure(figsize=(20,20),dpi=10)
#使用絕對(duì)路徑獲取字體的名稱的方法
fontname=font_manager.FontProperties(
? ? ? ? fname="C://Windows//Fonts//FZSTK.TTF")
#設(shè)置字體
font={
? ? ? 'family':fontname.get_name(),
? ? ? 'size':20}
matplotlib.rc('font',**font)
#設(shè)置橫軸與縱軸等長(zhǎng)的餅圖
plt.axis('equal')
#繪制餅圖
plt.pie(
? ? ? ? result['用戶數(shù)'],
? ? ? ? labels=result['通信品牌'],
? ? ? ? autopct='%.2f%%')
#設(shè)置突出的部分
explode=(0.1,0.2,0.3)
plt.axis('equal')
plt.pie(
? ? ? ? result['用戶數(shù)'],
? ? ? ? labels=result['通信品牌'],
? ? ? ? autopct='%.2f%%')結(jié)果為:

直方圖:


#直方圖
import pandas
import matplotlib
from matplotlib import pyplot as plt
#設(shè)置字體
font={
? ? ? 'family':'SimHei',
? ? ? 'size':15}
matplotlib.rc('font',**font)
#導(dǎo)入數(shù)據(jù)
data_histogram=pandas.read_csv('D://Python projects//reference data//6.5//data.csv')
maincolor=(42/256,87/256,141/256,1)
#繪制初步直方圖
plt.hist(data_histogram.購(gòu)買用戶數(shù),
? ? ? ? ?color=maincolor)
plt.hist(data_histogram['購(gòu)買用戶數(shù)'],
? ? ? ? ?color=maincolor)結(jié)果為:

#設(shè)置分組個(gè)數(shù)為30 plt.hist(data_histogram.購(gòu)買用戶數(shù), ? ? ? ? ?bins=(30), ? ? ? ? ?color=maincolor)
結(jié)果為:

#繪制瀑布圖,即累計(jì)計(jì)算 plt.hist(data_histogram.購(gòu)買用戶數(shù), ? ? ? ? ?bins=(30), ? ? ? ? ?cumulative=True, ? ? ? ? ?color=maincolor)
結(jié)果為:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pytorch損失函數(shù)torch.nn.NLLLoss()的使用
這篇文章主要介紹了Pytorch損失函數(shù)torch.nn.NLLLoss()的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
python interpret庫(kù)訓(xùn)練模型助力機(jī)器學(xué)習(xí)
這篇文章主要為大家介紹了python interpret庫(kù)訓(xùn)練模型功能特性,為你的機(jī)器學(xué)習(xí)提供便捷的路徑,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
tensorflow 實(shí)現(xiàn)自定義梯度反向傳播代碼
今天小編就為大家分享一篇tensorflow 實(shí)現(xiàn)自定義梯度反向傳播代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
在Django中動(dòng)態(tài)地過(guò)濾查詢集的實(shí)現(xiàn)
本文主要介紹了Django中動(dòng)態(tài)地過(guò)濾查詢集的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
python3.6.8 + pycharm + PyQt5 環(huán)境搭建的圖文教程
這篇文章主要介紹了python3.6.8 + pycharm + PyQt5 環(huán)境搭建,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
用python實(shí)現(xiàn)英文字母和相應(yīng)序數(shù)轉(zhuǎn)換的方法
這篇文章主要介紹了用python實(shí)現(xiàn)英文字母和相應(yīng)序數(shù)轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

