Python imports csv data to draw comparison histogram

Result graph:
Insert image description here

csv file data:
Insert image description here

Use pandas to import csv files and matplotlib to draw histograms:
some are shown below 内联代码片.


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sys
# 遇到数据中有中文的时候,一定要先设置中文字体
#plt.rcParams['font.sans-serif']=['SimHei'] # 用黑体显示中文
plt.rcParams['axes.unicode_minus'] = False

disktype = 'CLOUD_SSD'
env = 'LAN'
row = '100W'

def auto_data(rects):
    plt.bar_label(rects, fmt='%g', label_type='edge', size=10, family="Times new roman", padding=1)


#筛选出想要的列和数据
df = pd.read_csv('qps.csv')
df= df[(df['disktype'] == f'{
      
      disktype}') & (df['env'] == f'{
      
      env}') & (df['row'] == f'{
      
      row}') ]
df = df[['benchmode','threads', 'qps']]
df = df.sort_values(['benchmode','threads'],ascending=[False,True])
#print(df)


name_list = df.columns.tolist() # 获取该dataframe的属性名称(此处应该为benchmode,threads,qbs),此处已转化为list格式
print('name_list:',name_list)

#过滤出benchmode_type
benchmode_type = df[name_list[0]].drop_duplicates(keep='first', inplace=False).tolist()

#将threads、qps转化为int格式
df[name_list[1]] = df[name_list[1]].astype(int) # 转化为int
df[name_list[2]] = df[name_list[2]].astype(int) # 转化为int

#获取最大qpsY轴数值
max_Y = df[name_list[2]].max()

#将qps和threads变成数组
data_0 = df[df[name_list[0]] == benchmode_type[0]]  # 数据表1
data_1 = df[df[name_list[0]] == benchmode_type[1]]  # 数据表2
#print(data_0)
#print(data_1)
list_0 = np.array(data_0[name_list[2]]) # 读取threads数据
list_1 = np.array(data_1[name_list[2]]) # 读取qps
print(list_0)
print(list_1)

list_threads = list(set(df[name_list[1]])) # 读取threads的类型
list_threads.sort()
print(list_threads)   # 横坐标

length = len(list_0)  # 横坐标个数
x = np.arange(length)  # 横坐标范围

plt.figure()
total_width, n = 0.8, 2   # 柱状图总宽度,有几组数据
width = total_width / n   # 单个柱状图的宽度
x0 = x - 0.5 * width    # 第一组数据柱状图横坐标起始位置
x1 = x0 + width   # 第二组数据柱状图横坐标起始位置

plt.title(f"{
      
      env}_{
      
      disktype} QPS")    # 图标题
plt.xlabel(name_list[1])    # x轴标题
plt.ylabel(name_list[2])    # y轴标题
cm = plt.bar(x0, data_0[name_list[2]], width, label=benchmode_type[0])   # 画柱状图
auto_data(cm)
cm =plt.bar(x1, data_1[name_list[2]], width, label=benchmode_type[1])   # 画柱状图
auto_data(cm)

plt.xticks(x, list_threads)  # 用threads的类型替换横坐标x的值
plt.ylim(0,max_Y+max_Y*0.3) # y轴范围(0~20000)
# plt.yticks(np.linspace(0, 20000, 5))  # y轴刻度(0~20000,分成5刻度)

plt.legend()   # 给出图例
plt.show()

Result graph:
Insert image description here

Guess you like

Origin blog.csdn.net/qq_38818972/article/details/129087439