del font['cmap'].tables[0].cmap[unicode_val]对吗
时间: 2024-01-06 08:04:11 浏览: 29
这个代码是用来删除字体文件中指定unicode码点对应的字符映射记录的。其中,`font`是一个字体对象,`font['cmap']`表示获取字体对象中的字符映射表,`.tables[0]`表示获取字符映射表中的第一个子表,`.cmap[unicode_val]`表示获取指定unicode码点对应的字符映射记录,最后,`.tables[0].cmap[unicode_val]`删除了这个字符映射记录。
相关问题
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,怎么修改
这个错误提示通常是由于数据中含有非数值型的字符串导致的。你可以尝试将非数值型的列进行排除或者转化为数值型,例如:
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()
```
其中,第一种方法是直接排除非数值型的列,第二种方法是将非数值型的列转化为数值型,以便于进行可视化。
class RandomWalk(): def __init__(self,point_num=5000): self.point_num = point_num self.xval = [0] self.yval = [0] def fill_walk(self): while len(self.xval)<self.point_num: x_direction = choice([1,-1]) step_num = choice([0,1,2,3,4]) xstep = x_direction * step_num y_direction = choice([1,-1]) step_num = choice([0,1,2,3,4]) ystep = y_direction * step_num if xstep == 0 and ystep==0: continue x_next = self.xval[-1] + xstep y_next = self.yval[-1] + ystep self.xval.append(x_next) self.yval.append(y_next) rw = RandomWalk(50000) rw.fill_walk() point_numbers = list(range(rw.point_num())) plt.scatter(rw.x_val,rw.y_val,c=point_numbers,cmap=plt.cm.Blues,edgecolor = 'none',s=2) plt.scatter(0,0,c='green',edgecolors='none',s=100) plt.scatter(rw.x_val[-1],rw.y_val[-1],c='red',edgecolor='none',s=100) plt.axes().get_xaxis().set_visible(False) plt.axes().get_xaxis().set_yisible(False) plt.show()修改代码
以下是修改后的代码:
```python
import matplotlib.pyplot as plt
from random import choice
class RandomWalk():
def __init__(self, point_num=5000):
self.point_num = point_num
self.x_val = [0]
self.y_val = [0]
def fill_walk(self):
while len(self.x_val) < self.point_num:
x_direction = choice([1, -1])
x_step = choice([0, 1, 2, 3, 4])
x_val = x_direction * x_step
y_direction = choice([1, -1])
y_step = choice([0, 1, 2, 3, 4])
y_val = y_direction * y_step
if x_val == 0 and y_val == 0:
continue
next_x = self.x_val[-1] + x_val
next_y = self.y_val[-1] + y_val
self.x_val.append(next_x)
self.y_val.append(next_y)
rw = RandomWalk(50000)
rw.fill_walk()
point_numbers = list(range(rw.point_num))
plt.scatter(rw.x_val, rw.y_val, c=point_numbers, cmap=plt.cm.Blues, edgecolor='none', s=2)
plt.scatter(0, 0, c='green', edgecolors='none', s=100)
plt.scatter(rw.x_val[-1], rw.y_val[-1], c='red', edgecolor='none', s=100)
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
```
主要的修改包括:
1. `xval` 和 `yval` 改为 `x_val` 和 `y_val`,以符合 Python 的命名规范。
2. `rw.point_num()` 改为 `rw.point_num`,因为 `point_num` 是一个属性而不是方法。
3. `plt.axes().get_xaxis().set_yisible(False)` 改为 `plt.axes().get_yaxis().set_visible(False)`,因为原来的代码中打错了单词。