python excel画图,使用python操作excel画图

该博客介绍了一种使用Python的openpyxl库读取Excel数据,去除空值,计算数据频率,并利用这些数据绘制雷达图的方法。首先,通过read_all_data函数读取所有数据并处理NaN值,然后使用caculate_frequence计算每个数据项的频率,最后通过draw_picture绘制雷达图。整个过程展示了如何在Python中进行数据处理和可视化。
摘要由CSDN通过智能技术生成

from openpyxl import load_workbook

from openpyxl.chart import (

RadarChart,

Reference,

)

import numpy as np

def read_all_data(worksheet):

print("read_all_data...")

data_list = []

for row in worksheet.values:

for value in row:

data_list.append(value)

print("data_list",data_list)

dl = data_list

return dl

def caculate_frequence(dl):

print("caculate_frequence...")

data_small_large = sorted(dl)

#print("data",data)

unique_data = np.unique(data_small_large)

print("unique_data",unique_data)

data_times = []

for i in unique_data:

data_times.append(data_small_large.count(i))

print("resdata",resdata)

times_total = sum(data_times)

print("times_total",times_total)

data_freq = []

for i in data_times:

freq_i = i / times_total

data_freq.append(freq_i)

df = data_freq

print("df",df)

return df

def draw_picture(df,dl):

print("draw_picture...")

for row in df:

ws.append(row)

chart = RadarChart()

chart.type = "filled"

#labels = Reference(ws, min_col=1, min_row=2, max_row=13)

data = Reference(ws, min_col=1, max_col=1, min_row=1, max_row=len(dl))

chart.add_data(data, titles_from_data=False)

#chart.set_categories(labels)

chart.style = 26

chart.title = "wind direction frequence"

chart.y_axis.delete = True

ws.add_chart(chart, "B1")

wb.save(r"D:\MyDocument\excel\pic\wind_direction_pic_1978.xlsx")

def no_none(dl):

print("no_none...")

for item in dl[:]:

if item == None:

dl.remove(item)

return dl

if __name__=="__main__":

file_name = r'D:\MyDocument\excel\wind_direction_' + str(78) + '.xlsx' # 读取数据的文件名

wb = load_workbook(file_name) # 加载工作本

#print(wb.sheetnames)

ws = wb.active # 获取Sheet1

data_list = read_all_data(ws) # 读取所有的data

data_list = no_none(data_list) # 处理NaN(空值)

data_freq = caculate_frequence(data_list) # 计算频率

draw_picture(data_freq,data_list) # 画图

标签:dl,python,list,excel,画图,chart,print,data,row

来源: https://blog.csdn.net/weixin_44638075/article/details/114555881

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值