python繪制直方圖的方法
更新時(shí)間:2022年04月21日 14:55:11 作者:lengedd
這篇文章主要為大家詳細(xì)介紹了python繪制直方圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python繪制直方圖的具體代碼,供大家參考,具體內(nèi)容如下
用兩列數(shù)據(jù)繪制直方圖

#coding=gbk
import xlwings as xw
import pandas ?as pd
import matplotlib.pyplot as plt
#pd.set_option('display.max_columns', None) ?#解決表格多列時(shí)中間省略顯示問(wèn)題
#pd.set_option('display.max_rows', None) ? ?#解決表格多行時(shí)中間省略顯示問(wèn)題
#讀取excel文件中的數(shù)據(jù)
app = xw.App(visible = False, add_book = False)
workbook = app.books.open("score1000.xlsx")
worksheet = workbook.sheets[0] ?#使用sheets()方法獲取所有sheet頁(yè),加個(gè)序號(hào)獲取某個(gè)sheet頁(yè)
values = worksheet.range("A1").expand().options(pd.DataFrame, index = False).value
print(values)
workbook.close()
app.quit()
#繪制直方圖
figure = plt.figure()
#plt.rcParams['font.sans-serif'] = ['SimHei'] ?#解決圖表中中文顯示問(wèn)題
#plt.rcParams['axes.unicode_minus'] = False ? #解決圖表中負(fù)號(hào)顯示問(wèn)題
x = values['total_score'] ?#指定X軸
y = values['interface_delta_B'] ?#指定Y軸
plt.bar(x, y, color = 'blue')
#設(shè)置圖表參數(shù)
plt.xlabel('total_score', fontsize = 15, color = 'black') ? #設(shè)置x軸標(biāo)簽
plt.ylabel('interface_delta_B', fontsize = 15, color = 'green') ? #設(shè)置y軸標(biāo)簽
#plt.title('score', fontsize = 20) ?#設(shè)置標(biāo)題
#plt.axis([-1, 6, -2, 2]) ? #可手動(dòng)設(shè)置x軸y軸范圍
#plt.grid(True) ? #設(shè)置網(wǎng)格
plt.show()
用一列數(shù)據(jù)繪制直方圖
# coding=gbk
import pandas as pd
import matplotlib.pyplot as plt
from pyecharts import options as opts
from pyecharts.charts import Bar
import numpy as np
df = pd.read_excel("score1000.xlsx",engine='openpyxl')
#print(df["total_score"])
#使用matplotlib畫圖
# plt.figure()
# plt.hist(df["interface_delta_B"])
# plt.show()
hist,bin_edges = np.histogram(df["interface_delta_B"],bins=100)
# # print(bin_edges)
# # print(len(bin_edges))
# # print(len(hist))
bar=(
? ? Bar()
? ? .add_xaxis([str(x) for x in bin_edges[:-1]])
? ? .add_yaxis("",[float(x) for x in hist],category_gap=0)
? ? .set_global_opts(
? ? ? ? title_opts=opts.TitleOpts(title="interface_delta_B",pos_left="center"),
? ? ? ? legend_opts=opts.LegendOpts(is_show=False)
? ? )
)
bar.render("F:total_score.html")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)修改阿里云DNS域名解析
當(dāng)公網(wǎng)IP是浮動(dòng)的時(shí)候,用一個(gè)域名去實(shí)時(shí)解析,才不會(huì)那么糟糕,本文將介紹如何使用python修改阿里云dns域名解析,感興趣的小伙伴可以了解一下2024-11-11
詳解用TensorFlow實(shí)現(xiàn)邏輯回歸算法
本篇文章主要介紹了詳解用TensorFlow實(shí)現(xiàn)邏輯回歸算法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Python使用Tkinter實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)器的步驟詳解
這篇文章主要介紹了Python使用Tkinter實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)器,,本文分場(chǎng)景通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
淺析Python+OpenCV使用攝像頭追蹤人臉面部血液變化實(shí)現(xiàn)脈搏評(píng)估
這篇文章主要介紹了Python+OpenCV使用攝像頭追蹤人臉面部血液變化實(shí)現(xiàn)脈搏評(píng)估,本文通過(guò)一段代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Python requests請(qǐng)求超時(shí)的解決方案
在進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)爬取過(guò)程中,網(wǎng)絡(luò)請(qǐng)求超時(shí)是一個(gè)令人頭疼的問(wèn)題,尤其在Python中,我們常常需要應(yīng)對(duì)各種網(wǎng)絡(luò)爬蟲(chóng)、API調(diào)用或其他網(wǎng)絡(luò)操作,而網(wǎng)絡(luò)請(qǐng)求超時(shí)的原因千奇百怪,在本篇文章中,我們將深入探討Python requests請(qǐng)求超時(shí)的解決方案,需要的朋友可以參考下2024-12-12

