import pandas as pd import matplotlib.pyplot as plt # 读取Excel1和Excel2的数据 df1 = pd.read_excel('C:\\Users\\ASUS\\Desktop\\2023310道旅.xlsx') df2 = pd.read_excel('C:\\Users\\ASUS\\Desktop\\2023310危险品.xlsx') # 绘制箱型图 fig, ax = plt.subplots() ax.boxplot([df1['道旅报警数/百公里'], df2['危险品报警数/百公里']]) ax.set_xticklabels(['道路旅客', '危险品']) ax.set_ylabel('安全态势') plt.show()对这段代码修改字体
时间: 2023-07-21 12:23:55 浏览: 123
可以使用前面提到的方法之一,比如修改字体。以下是修改字体的代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 读取Excel1和Excel2的数据
df1 = pd.read_excel('C:\\Users\\ASUS\\Desktop\\2023310道旅.xlsx')
df2 = pd.read_excel('C:\\Users\\ASUS\\Desktop\\2023310危险品.xlsx')
# 绘制箱型图
fig, ax = plt.subplots()
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)
ax.boxplot([df1['道旅报警数/百公里'], df2['危险品报警数/百公里']], labels=['道路旅客', '危险品'], fontproperties=font)
ax.set_ylabel('安全态势', fontproperties=font)
plt.show()
```
其中,`FontProperties` 指定了字体文件路径和大小,`labels` 参数直接指定标签,`set_ylabel` 方法也指定了字体。这里仍然使用了宋体字体 `simhei.ttf`,你可以根据需要修改。
相关问题
#%% import pandas as pd import numpy as np import matplotlib.pyplot as plt # 读取Excel文件中的所有表格 excel_file = pd.ExcelFile('附件1-装箱数据.xlsx') # 读取指定表格 df1 = pd.read_excel(excel_file, sheet_name='订单数据') df2 = pd.read_excel(excel_file, sheet_name='耗材数据') # 检查是否有缺失值 print(df1.isnull().sum()) print(df2.isnull().sum()) print(df1.head()) print(df2.head()) df2=df2['耗材类型'].replace({'袋':1, '箱': 0}, inplace=True) df2.drop(columns=['重量']).values print(df1.head()) print(df2.head())
这段代码主要是对Excel文件中的两个表格进行读取,并进行了一些简单的数据处理和检查缺失值的操作。具体解释如下:
- 首先,通过 pandas 库中的 ExcelFile 类读取 Excel 文件中的所有表格。
- 然后,使用 read_excel() 方法读取指定的表格,并将其存储为 DataFrame 对象,分别命名为 df1 和 df2。
- 接着,使用 isnull() 和 sum() 方法检查 df1 和 df2 中是否有缺失值。
- 然后,使用 head() 方法查看 df1 和 df2 的前几行数据,以确保读取正确。
- 接下来,对 df2 中的耗材类型进行转换,转换为数字表示,袋为1,箱为0。
- 然后,使用 drop() 方法删除 df2 中的重量列,并使用 values 属性将其转换为 NumPy 数组。
- 最后,再次使用 head() 方法查看 df1 和 df2 的前几行数据,以确保数据处理和转换正确。
需要注意的是,代码中的 df2=df2['耗材类型'].replace({'袋':1, '箱': 0}, inplace=True) 这一行不是很合理,因为它实际上没有对 df2 进行任何操作。正确的写法应该是 df2['耗材类型'].replace({'袋':1, '箱': 0}, inplace=True)。
import pandas as pd from sklearn.manifold import TSNE import matplotlib.pyplot as plt # 读取数据集 customer = pd.read_csv('customer.csv',encoding='gbk') customer_data = customer.iloc[:,:-1] customer_target = customer.iloc[:,-1] # K-Means类聚 from sklearn.cluster import KMeans kmeans = KMeans(n_clusters=4,random_state=6).fit((customer_data)) # 使用TSNE进行数据降维,降成2维 tsne = TSNE(n_components=2,init='random', random_state=2).fit(customer_data) df = pd.DataFrame(tsne.embedding_) # 提取不同标签的数据 df1 = df[df['labels'] == 0] df2 = df[df['labels'] == 1] df3 = df[df['labels'] == 2] df4 = df[df['labels'] == 3] # 绘制图像 fig = plt.figure(figsize=(9,6)) # 用不同颜色表示不同数据 plt.plot(df1[0],df1[1],'bo',df2[0],df2[1],'r*', df3[0],df1[1],'gD',df4[0],df4[1],'kD') plt.show()这段代码有什么问题
这段代码有两个问题:
1. 在使用KMeans进行聚类时,没有将聚类的标签赋值给数据集,导致后续无法根据标签提取不同标签的数据。
2. 在绘制图像时,df3的y轴坐标应该是df3[1]而不是df1[1]。
以下是修改后的代码:
```python
import pandas as pd
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
# 读取数据集
customer = pd.read_csv('customer.csv', encoding='gbk')
customer_data = customer.iloc[:,:-1]
customer_target = customer.iloc[:,-1]
# K-Means类聚
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=4, random_state=6).fit(customer_data)
labels = kmeans.labels_
# 使用TSNE进行数据降维,降成2维
tsne = TSNE(n_components=2, init='random', random_state=2).fit(customer_data)
df = pd.DataFrame(tsne.embedding_)
df['labels'] = labels
# 提取不同标签的数据
df1 = df[df['labels'] == 0]
df2 = df[df['labels'] == 1]
df3 = df[df['labels'] == 2]
df4 = df[df['labels'] == 3]
# 绘制图像
fig = plt.figure(figsize=(9,6))
plt.plot(df1[0], df1[1], 'bo', df2[0], df2[1], 'r*', df3[0], df3[1], 'gD', df4[0], df4[1], 'kD')
plt.show()
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)