用python matplotlib读取E:\\爬取\\爬取.xls文件,画文件前五条数据的标题预售价(标题去掉括号)的雷达图
时间: 2023-06-30 20:14:03 浏览: 60
好的,以下是代码实现:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取Excel文件
file_path = r"E:\爬取\爬取.xls"
df = pd.read_excel(file_path)
# 取前五条数据的标题和预售价
data = df.loc[:4, ["标题", "预售价"]]
# 去掉标题中的括号
data["标题"] = data["标题"].str.replace(r"\(.*?\)", "", regex=True)
# 构造雷达图数据
labels = data["标题"].tolist()
values = data["预售价"].tolist()
# 绘制雷达图
angles = [n / float(len(labels)) * 2 * 3.1415 for n in range(len(labels))]
angles += angles[:1]
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(angles, values, 'o-', linewidth=2)
ax.fill(angles, values, alpha=0.25)
ax.set_thetagrids(angles[:-1], labels)
plt.title("前五条数据的标题预售价雷达图")
plt.show()
```
解释一下代码:
首先,使用 `pandas` 的 `read_excel` 函数读取 Excel 文件的内容,并取出前五条数据的标题和预售价,存储到 `data` 变量中。
然后,使用 `replace` 函数去掉标题中的括号。
接着,构造雷达图数据,其中 `labels` 变量存储标题,`values` 变量存储预售价。
最后,使用 `matplotlib` 绘制雷达图,使用 `set_thetagrids` 函数设置刻度标签,使用 `title` 函数设置图表标题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)