通过学习以及完成将txt转excel通过python导出所需数据。接下来需将所需数据进行玫瑰图出图,通过年月日小时四个字典以及有效波高以及方向作为三个元素进行玫瑰图出图。暂时没有思路麻烦可以点拨一下方向
import pandas as pd
import numpy as np
import xlrd
from matplotlib import pyplot
info = []
def read_excel():
workbook = xlrd.open_workbook("20221210wave.xls.xls")
# workbook = xlrd.open_workbook(r'E:\pyclass\202206\20221210wave.xls.xls')
sheet = workbook.sheet_by_index(0)
for i in range(1, sheet.nrows):
dictname = {"年": "", "月": "", "日": "", "小时": "", "有效波高": "", "方向": ""}
dictname["年"] = sheet.cell_value(i, 0)
dictname["月"] = sheet.cell_value(i, 1)
dictname["日"] = sheet.cell_value(i, 2)
dictname["小时"] = sheet.cell_value(i, 3)
dictname["有效波高"] = sheet.cell_value(i, 7)
dictname["方向"] = sheet.cell_value(i, 10)
info.append(dictname)
for item in info:
print(item)
read_excel()
https://www.bilibili.com/video/BV1x5411t7s3/?spm_id_from=333.880.my_history.page.click
已经学习构建玫瑰图,不知如何将字典内数据运用进去
希望可以以方向为变量,得到对应的有效波高以及时间
这玫瑰图好高级啊
如有帮助,望采纳
import xlwt
def txt2xls(filename,xlsname):
f = open(filename) #打开txt文本进行读取
x = 0 #在excel开始写的位置(y)
y = 0 #在excel开始写的位置(x)
xls=xlwt.Workbook()
sheet = xls.add_sheet('sheet1',cell_overwrite_ok=True)
while True: #循环,读取文本里面的所有内容
line = f.readline() #一行一行读取
if not line: #如果没有内容,则退出循环
break
for i in line.split('\t'):#读取出相应的内容写到x
# item=i.strip().decode('utf8')
item=i.strip()
sheet.write(x,y,item)
y += 1 #另起一列
x += 1 #另起一行
y = 0 #初始成第一列
f.close()
xls.save(xlsname+'.xls') #保存
filename='E:\\pycharmcommunity\project\\txt_to_excel\\fi-120-050.txt'
xlsname='fi-120-050'
txt2xls(filename,xlsname)
```python
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
# 读入数据,需要更改
df = pd.read_excel(r"C:\Users\lpf_a\PycharmProjects\ybt_clan\untitled1\30国.xlsx")
df = df.sort_values("累计")
v = df['疫情地区'].values.tolist()
d = df['累计'].values.tolist()
# 设置颜色
color_series = ['#FAE927', '#E9E416', '#C9DA36', '#9ECB3C', '#6DBC49',
'#37B44E', '#3DBA78', '#14ADCF', '#209AC9', '#1E91CA',
'#2C6BA0', '#2B55A1', '#2D3D8E', '#44388E', '#6A368B'
'#7D3990', '#A63F98', '#C31C88', '#D52178', '#D5225B',
'#D02C2A', '#D44C2D', '#F57A34', '#FA8F2F', '#D99D21',
'#CF7B25', '#CF7B25', '#CF7B25']
# 实例化Pie类
pie1 = Pie(init_opts=opts.InitOpts(width='1350px', height='750px'))
# 设置颜色
pie1.set_colors(color_series)
# 添加数据,设置饼图的半径,是否展示成南丁格尔图
pie1.add("222", [list(z) for z in zip(v, d)],
radius=["15%", "100%"],
center=["50%", "60%"],
rosetype="area"
)
# 设置全局配置项
pie1.set_global_opts(title_opts=opts.TitleOpts(title='玫瑰图示例'),
legend_opts=opts.LegendOpts(is_show=False),
toolbox_opts=opts.ToolboxOpts())
# 设置系列配置项
pie1.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="inside", font_size=12,
formatter="{b}:{c}例", font_style="italic",
font_weight="bold", font_family="Microsoft YaHei"
),
)
# 生成html文档
pie1.render("南丁格尔玫瑰图.html")
