# 绘制主题平均余弦相似度图形 from matplotlib.font_manager import FontProperties font = FontProperties(size=14) fig = plt.figure(figsize=(10,8)) ax1 = fig.add_subplot(211) ax1.plot(pos_k) ax1.set_xlabel('正面评论LDA主题数寻优', fontproperties=font) ax2 = fig.add_subplot(212) ax2.plot(neg_k) ax2.set_xlabel('负面评论LDA主题数寻优', fontproperties=font)标题有一些被方框替代,请修改代码
时间: 2023-08-05 17:10:31 浏览: 41
标题被方框替代通常是因为字体不支持中文字符。你可以尝试更改字体或者使用Unicode编码来解决这个问题。以下是一个示例代码:
```
from matplotlib.font_manager import FontProperties
# 更改字体
font = FontProperties(fname='SimHei.ttf', size=14) # 指定SimHei字体
fig = plt.figure(figsize=(10,8))
ax1 = fig.add_subplot(211)
ax1.plot(pos_k)
ax1.set_xlabel('正面评论LDA主题数寻优', fontproperties=font)
ax1.set_title('正面评论LDA主题数寻优平均余弦相似度', fontproperties=font)
ax2 = fig.add_subplot(212)
ax2.plot(neg_k)
ax2.set_xlabel('负面评论LDA主题数寻优', fontproperties=font)
ax2.set_title('负面评论LDA主题数寻优平均余弦相似度', fontproperties=font)
```
在这个代码中,我们指定了SimHei字体作为字体,这个字体支持中文字符。你也可以指定其他支持中文字符的字体。如果你不想更改字体,你可以使用Unicode编码来替换中文字符,这样也可以避免标题被方框替代的问题。
相关问题
import pandas as pd import warnings import sklearn.datasets import sklearn.linear_model import matplotlib import matplotlib.font_manager as fm import matplotlib.pyplot as plt import numpy as np import seaborn as sns data = pd.read_excel(r'C:\Users\Lenovo\Desktop\data.xlsx') print(data.info()) fig = plt.figure(figsize=(10, 8)) sns.heatmap(data.corr(), cmap="YlGnBu", annot=True) plt.title('相关性分析热力图') plt.rcParams['axes.unicode_minus'] = False plt.rcParams['font.sans-serif'] = 'SimHei' plt.show() y = data['y'] X = data.drop(['y'], axis=1) print('************************输出新的特征集数据***************************') print(X.head()) from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) def relu(X): output=np.maximum(0, X) return output def relu_back_propagation(derror_wrt_output,X): derror_wrt_dinputs = np.array(derror_wrt_output, copy=True) derror_wrt_dinputs[x <= 0] = 0 return derror_wrt_dinputs def activated(activation_choose,X): if activation_choose == 'relu': return relu(X) def activated_back_propagation(activation_choose, derror_wrt_output, output): if activation_choose == 'relu': return relu_back_propagation(derror_wrt_output, output) class NeuralNetwork: def __init__(self, layers_strcuture, print_cost = False): self.layers_strcuture = layers_strcuture self.layers_num = len(layers_strcuture) self.param_layers_num = self.layers_num - 1 self.learning_rate = 0.0618 self.num_iterations = 2000 self.x = None self.y = None self.w = dict() self.b = dict() self.costs = [] self.print_cost = print_cost self.init_w_and_b()
这段代码实现了一个神经网络类 NeuralNetwork,用于训练数据并进行预测。首先,通过 Pandas 库读取一个 Excel 文件中的数据,并使用 Seaborn 库绘制数据的相关性分析热力图。然后,将数据集分为训练集和测试集,用于训练和评估模型。
接下来,定义了一个 relu() 函数和 relu_back_propagation() 函数,用于实现神经网络的激活函数和反向传播过程。其中,relu() 函数实现了 ReLU 激活函数,即将输入 X 中小于 0 的值置为 0,大于等于 0 的值保持不变;relu_back_propagation() 函数则实现了 ReLU 函数的反向传播过程,即计算误差关于输入的偏导数。
接着,定义了一个 NeuralNetwork 类,并实现了初始化方法 __init__()、初始化权重和偏置参数的方法 init_w_and_b(),以及训练模型的方法 train()。其中,__init__() 方法接受一个层次结构列表 layers_strcuture,用于指定神经网络的构成;init_w_and_b() 方法用于初始化神经网络的权重和偏置参数;train() 方法用于训练神经网络,并返回训练好的参数。
在 NeuralNetwork 类的 __init__() 方法中,首先初始化了神经网络的层数、学习率、迭代次数等参数。然后,定义了神经网络的权重参数和偏置参数,存储在类的实例变量 w 和 b 中。最后,定义了一个成本函数的列表 costs,存储训练过程中每次迭代的成本值。
from matplotlib import font_manager as fm import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #解决中文乱码 plt.style.use('ggplot') from matplotlib import cm #原始数据 shapes = ['天津', '江西省', '安徽省', '云南省', '福建省', '河南省', '辽宁省', '重庆', '湖南省', '四川省', '北京', '上海', '广西壮族自治区', '河北省', '浙江省', '江苏省', '湖北省', '山东省', '广东省'] values = [287,383,842,866,1187,1405,1495,1620,1717, 2313,2378,3070,4332,5841,6482,7785,9358,9818,20254] s = pd.Series(values, index=shapes) labels = s.index sizes = s.values fig, ax = plt.subplots(figsize=(6,6)) # 设置绘图区域大小 colors = cm.rainbow(np.arange(len(sizes))/len(sizes)) # 颜色地图:秋天→彩虹→灰色→春天→黑色 patches, texts, autotexts = ax.pie(sizes, labels=labels, autopct='%1.0f%%', shadow=False, startangle=170, colors=colors) ax.axis('equal') ax.set_title('各地区线上图书销售占比图',loc='left') # 重新设置字体大小 proptease = fm.FontProperties() # 字体大小(从小到大): xx-small、x-small、small、medium、large、x-large、xx-large,或者是数字,如18 proptease.set_size('small') plt.setp(autotexts, fontproperties=proptease) plt.setp(texts, fontproperties=proptease) plt.show()
这段代码使用了matplotlib库来绘制一个饼图,表示各地区线上图书销售的占比情况。首先,导入了需要的库,并设置了中文字体和绘图样式。
然后,定义了原始数据,将地区名称存储在列表`shapes`中,将销售数量存储在列表`values`中。使用`pd.Series`函数将数据转换为Series对象。
接下来,创建一个Figure对象和一个Axes对象,并设置绘图区域的大小。
使用`cm.rainbow`生成颜色地图,将颜色与销售数量相关联。
使用`ax.pie`函数绘制饼图,传入销售数量和地区名称作为参数。设置`autopct='%1.0f%%'`来显示百分比标签。其他参数包括阴影效果、起始角度、颜色等。
通过设置`ax.axis('equal')`使饼图呈现圆形。
使用`ax.set_title`设置标题为"各地区线上图书销售占比图",并将其左对齐。
接下来,通过调整字体大小的方式,重新设置了标签的字体大小,以适应图形的显示。
最后,使用`plt.show()`显示图形。
运行这段代码,您将看到一个饼图,表示各地区线上图书销售的占比情况。
相关推荐
![](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)