绘制柱状图
import matplotlib.pyplot def 订购时间绘图(): global 订购时间 ''' 每一天的,X轴24个点位; 以铃音为单位,每个日期为x轴 ''' color_list=["red","orange","yellow","green","blue","purple",] for toneid,value in 订购时间.items(): total_width, n = 1, len(订购时间[toneid]) width = total_width / n #每根柱子的宽度 matplotlib.pyplot.clf() #清空 matplotlib.rc("font",family='FangSong') index=0 for 日期,times in value.items(): label_str=日期.replace(' 00:00:00','') time_dict=订购时间划分(times) num_list=time_dict.values() time_list=time_dict.keys() x=[int(t)-(total_width-width)/n + width*index for t in time_list] matplotlib.pyplot.bar(x,num_list,width=width,align='edge',color=color_list[index],label=label_str) matplotlib.pyplot.legend() #使得图例生效 index+=1 matplotlib.pyplot.title('{}-订购'.format(toneid)) matplotlib.pyplot.xlabel('小时') matplotlib.pyplot.ylabel('数量') matplotlib.pyplot.savefig(r"D:\BaiduSyncdisk\01工作\脚本\data\绘图\{}.png".format(toneid));
绘制折线图
import matplotlib.pyplot def draw_charts_v1(hostname:str,datetime_flag:int)->None: ''' 本函数使用百分百绘制图表 hostname:主机名 datetime_flag:时间范围标志 0:最近24小时 1:最近一周 2:最近一个月 ''' global result_dict,datetime_flag_dict data_dict = result_dict[hostname] # print(matplotlib.pyplot.style.available) matplotlib.pyplot.clf() #清空 ''' ['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-v0_8', 'seaborn-v0_8-bright', 'seaborn-v0_8-colorblind', 'seaborn-v0_8-dark', 'seaborn-v0_8-dark-palette', 'seaborn-v0_8-darkgrid', 'seaborn-v0_8-deep', 'seaborn-v0_8-muted', 'seaborn-v0_8-notebook', 'seaborn-v0_8-paper', 'seaborn-v0_8-pastel', 'seaborn-v0_8-poster', 'seaborn-v0_8-talk', 'seaborn-v0_8-ticks', 'seaborn-v0_8-white', 'seaborn-v0_8-whitegrid', 'tableau-colorblind10'] ''' matplotlib.pyplot.style.use('bmh') #图形风格:bmh、ggplot、dark_background、fivethirtyeight和grayscale。 matplotlib.pyplot.figure(figsize=(16,10)) #长宽,单位为百像素。即16表示1600像素 matplotlib.rc("font",family='FangSong') #字体 #matplotlib.pyplot.xticks(rotation=1) #X轴间隔 matplotlib.pyplot.yticks(numpy.linspace(0,1.2,num=13)) #Y轴间隔 matplotlib.pyplot.ylim(0,1.2) #调整Y轴值范围 server_state = [int(x) for x in data_dict['server_state']] #转换下类型才不会出问题。 # server_state_y = (server_state-numpy.min(server_state))/(numpy.max(server_state)-numpy.min(server_state)) matplotlib.pyplot.plot(data_dict['reg_date'],server_state,label='服务状态') matplotlib.pyplot.plot(data_dict['reg_date'],data_dict['活跃线程占比'],label='活跃线程占比') matplotlib.pyplot.plot(data_dict['reg_date'],data_dict['cpu使用率'],label='cpu使用率') matplotlib.pyplot.plot(data_dict['reg_date'],data_dict['内存使用率'],label='内存使用率') matplotlib.pyplot.plot(data_dict['reg_date'],data_dict['硬盘使用率'],label='硬盘使用率') pic_name='{} {} 状态折线图'.format(hostname,datetime_flag_dict[datetime_flag]) matplotlib.pyplot.title(pic_name) matplotlib.pyplot.xlabel('时间') matplotlib.pyplot.ylabel('百分比') matplotlib.pyplot.legend() matplotlib.pyplot.savefig(r"{}{}.png".format(check_folder(datetime_flag),pic_name)) matplotlib.pyplot.close(); #显式关闭。
将小图汇总生成大图
def Merge_pic(path:str,width:int,height:int,png_num:int=19): ''' 本函数用于将生成的所有图表合并成一张大图,生成的大图将以.jpg作为文件后缀 path:路径,里面应存储有待合并的图片 width:图表的宽度,单位像素 height:图表的高度,单位像素 png_num:图片总数,默认值19,将按照4*5的方式排列 ''' #获取图片名称 images_files = [f for f in os.listdir(path) if f.endswith('.png')] #初始化大图的宽度和高度 jpg_width=4*width jpg_height=5*height #创建一个空白的大图 new_image= PIL.Image.new('RGB',(jpg_width,jpg_height),'#FFFFFF') #初始化为白色 #设置初始位置 x_offset=0 y_offset=0 #循环读取每张图片并拼接到大图上 for y in range(5): for x in range(4): idx = x+y*4 if(19<=idx): continue img = PIL.Image.open(path + images_files[idx]) new_image.paste(img,(x_offset,y_offset)) #更新位置信息 x_offset += img.width x_offset=0 y_offset+=img.height #保存 new_image.save('{}所有主机-{}状态折线图.jpg'.format(path,os.path.basename(path.replace('\\',''))))
标签:seaborn,pyplot,matplotlib,生成,v0,width,柱状图,dict,图片 From: https://www.cnblogs.com/love-DanDan/p/18022579