python繪制直方圖的方法
更新時間:2022年04月21日 14:55:11 作者:lengedd
這篇文章主要為大家詳細(xì)介紹了python繪制直方圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(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) ?#解決表格多列時中間省略顯示問題
#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 ? #解決圖表中負(fù)號顯示問題
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)文章
Django REST framework內(nèi)置路由用法
這篇文章主要介紹了Django REST framework內(nèi)置路由用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
Python使用smtp和pop簡單收發(fā)郵件完整實(shí)例
這篇文章主要介紹了Python使用smtp和pop簡單收發(fā)郵件完整實(shí)例,簡單介紹了smtp和pop,然后分享了相關(guān)實(shí)例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
python http服務(wù)flask架構(gòu)實(shí)用代碼詳解分析
本篇文章主要分享一個python的簡單http服務(wù)flask架構(gòu)。目前主流的python的服務(wù)框架有django、flask,相較于django來說,flask更小巧玲瓏。至于并發(fā)的問題,使用了gevent協(xié)程io進(jìn)行處理2021-10-10

