亲宝软件园·资讯

展开

python绘制直方图

lengedd 人气:0

用两列数据绘制直方图

#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文件中的数据
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')

#设置图表参数
plt.xlabel('total_score', fontsize = 15, color = 'black')   #设置x轴标签
plt.ylabel('interface_delta_B', fontsize = 15, color = 'green')   #设置y轴标签
#plt.title('score', fontsize = 20)  #设置标题
#plt.axis([-1, 6, -2, 2])   #可手动设置x轴y轴范围
#plt.grid(True)   #设置网格

plt.show()

用一列数据绘制直方图

# 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")

加载全部内容

相关教程
猜你喜欢
用户评论