python繪制直方圖的方法
更新時間:2022年04月21日 14:55:11 作者:lengedd
這篇文章主要為大家詳細介紹了python繪制直方圖的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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) ?#解決表格多列時中間省略顯示問題
#pd.set_option('display.max_rows', None) ? ?#解決表格多行時中間省略顯示問題
#讀取excel文件中的數(shù)據(jù)
app = xw.App(visible = False, add_book = False)
workbook = app.books.open("score1000.xlsx")
worksheet = workbook.sheets[0] ?#使用sheets()方法獲取所有sheet頁,加個序號獲取某個sheet頁
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'] ?#解決圖表中中文顯示問題
#plt.rcParams['axes.unicode_minus'] = False ? #解決圖表中負號顯示問題
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]) ? #可手動設(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")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用Tkinter實現(xiàn)轉(zhuǎn)盤抽獎器的步驟詳解
這篇文章主要介紹了Python使用Tkinter實現(xiàn)轉(zhuǎn)盤抽獎器,,本文分場景通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
淺析Python+OpenCV使用攝像頭追蹤人臉面部血液變化實現(xiàn)脈搏評估
這篇文章主要介紹了Python+OpenCV使用攝像頭追蹤人臉面部血液變化實現(xiàn)脈搏評估,本文通過一段代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10

