import numpy as np import scipy.stats as ss import pandas as pd import matplotlib.pyplot as plt import seaborn as sns df=pd.read_excel("./test.xlsx") sns.set_context(font_scale=30) plt.rcParams['font.family'] = 'FangSong' plt.figure(figsize=(100,1000)) min_val = df.loc[:, ["FactoryName", "JiJXH"]].values.min() max_val = df.loc[:, ["FactoryName", "JiJXH"]].values.max() sns.heatmap(df,vmin=min_val, vmax=max_val,cmap=sns.color_palette("RdYlBu",n_colors=10),annot=True, fmt='g', annot_kws={"size": 20}) plt.show()提示could not convert string to float,怎么修改
时间: 2024-02-11 19:04:10 浏览: 142
Python科学画图代码分享
这个错误提示通常是由于数据中含有非数值型的字符串导致的。你可以尝试将非数值型的列进行排除或者转化为数值型,例如:
1. 将非数值型的列排除:
```python
df = pd.read_excel("./test.xlsx")
df_numeric = df.select_dtypes(include=[np.number]) # 选择数值型的列
sns.set_context(font_scale=30)
plt.rcParams['font.family'] = 'FangSong'
plt.figure(figsize=(100,1000))
min_val = df_numeric.values.min()
max_val = df_numeric.values.max()
sns.heatmap(df_numeric, vmin=min_val, vmax=max_val, cmap=sns.color_palette("RdYlBu",n_colors=10), annot=True, fmt='g', annot_kws={"size": 20})
plt.show()
```
2. 将非数值型的列转化为数值型:
```python
df = pd.read_excel("./test.xlsx")
df["FactoryName"] = pd.factorize(df["FactoryName"])[0] # 将非数值型的列转化为数值型
sns.set_context(font_scale=30)
plt.rcParams['font.family'] = 'FangSong'
plt.figure(figsize=(100,1000))
min_val = df.loc[:, ["FactoryName", "JiJXH"]].values.min()
max_val = df.loc[:, ["FactoryName", "JiJXH"]].values.max()
sns.heatmap(df, vmin=min_val, vmax=max_val, cmap=sns.color_palette("RdYlBu",n_colors=10), annot=True, fmt='g', annot_kws={"size": 20})
plt.show()
```
其中,第一种方法是直接排除非数值型的列,第二种方法是将非数值型的列转化为数值型,以便于进行可视化。
阅读全文