画饼图:
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import pandas as pd
import numpy as np
#import seaborn as
import matplotlib.pyplot as plt
import os
plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
plt.rcParams['axes.unicode_minus'] = False
file_path = r'C:/Users/admin/Desktop/catering_dish_profit.xls' # r对路径进行转义,windows需要
data = pd.read_excel(file_path) # header=0表示第一行是表头,就自动去除了
print(data)
print("\n")
y=data.iloc[1].values
y=y[1:]
print(y)
plt.pie(y,
labels=['A1','A2','A3','A4','A5','A6','A7','A8','A9','A10'], # 设置饼图标签
colors=["#d5695d", "#5d8ca8", "#65a479", "#a564c9","#d5695d", "#5d8ca8", "#65a479", "#a564c9","#d5695d", "#5d8ca8"], # 设置饼图颜色
explode=(0, 0.2, 0, 0,0,0,0,0,0,0), # 第二部分突出显示,值越大,距离中心越远
autopct='%.2f%%', # 格式化输出百分比
)
plt.show()
箱型图
import pandas as pd file_path = r'D:\linshi\catering_fish_congee.xls' # r对路径进行转义,windows需要 raw_data = pd.read_excel(file_path, header=0) # header=0表示第一行是表头,就自动去除了 print(raw_data) import matplotlib.pyplot as plt import pandas as pd import numpy as np #import seaborn as import matplotlib.pyplot as plt # 设置中文和负号正常显示 plt.rcParams['font.sans-serif'] = 'Microsoft YaHei' plt.rcParams['axes.unicode_minus'] = False # 设置图形的显示风格 plt.style.use('ggplot') # 绘图:daily_Ionset_r_c1_predicted的箱线图 data= pd.read_excel(file_path, header=0,usecols=[1]) print(data) plt.boxplot(x = data, # 指定绘图数据 patch_artist=True, # 要求用自定义颜色填充盒形图,默认白色填充 showmeans=True, # 以点的形式显示均值 boxprops = {'color':'black','facecolor':'#9999ff'}, # 设置箱体属性,填充色和边框色 flierprops = {'marker':'o','markerfacecolor':'red','color':'black'}, # 设置异常值属性,点的形状、填充色和边框色 meanprops = {'marker':'D','markerfacecolor':'indianred'}, # 设置均值点的属性,点的形状、填充色 medianprops = {'linestyle':'--','color':'orange'}) # 设置中位数线的属性,线的类型和颜色 # 设置y轴的范围 #plt.ylim(0,3000) # 去除箱线图的上边框与右边框的刻度标签 plt.tick_params(top='off', right='off') # 保存图像 plt.savefig('./results_imgs.png', bbox_inches='tight') #保存的文件名,裁剪掉图表多余的空白区域 # 显示图形 plt.show()
柱状图
# -*- coding: utf-8 -*- """ Created on Wed Feb 22 15:15:24 2023 @author: admin """ import pandas as pd import numpy as np #import seaborn as import matplotlib.pyplot as plt import os plt.rcParams['font.sans-serif'] = 'Microsoft YaHei' plt.rcParams['axes.unicode_minus'] = False file_path = r'C:/Users/admin/Documents/WeChat Files/wxid_z7gj7w9vk2wl32/FileStorage/File/2023-02/dish_sale_b(1).xls' # r对路径进行转义,windows需要 data = pd.read_excel(file_path) # header=0表示第一行是表头,就自动去除了 print(data) data=np.array(data) x=data[0:12,0] print(x) x1=data[0:12,1] print(x1) x2=data[0:12,2] print(x2) x3=data[0:12,3] print(x3) x_width = range(0,len(x)) x2_width = [i+0.3 for i in x_width] x3_width = [i+0.3 for i in x2_width] plt.bar(x_width,x1,lw=0.5,fc="r",width=0.3,label="2014") plt.bar(x2_width,x2,lw=0.5,fc="b",width=0.3,label="2015") plt.bar(x3_width,x3,lw=0.5,fc="y",width=0.3,label="2016") plt.xticks(range(0,5),x)
标签:plt,统计数据,Python,画图,width,file,print,import,data From: https://www.cnblogs.com/shuang3016/p/17157430.html