1、指定字体,解决中文无法显示
plt.rcParams['font.sans-serif'] = ['SimHei']
# 子图和x轴旋转
fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)
# 添加标题
ax1.set_title('情绪指数')
ax2.set_title('分歧指数')
# ax1.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
ax1.plot(x_split_list, y_mood_list)
# 获取每个横坐标,并且旋转45度
for tick in ax1.get_xticklabels():
tick.set_rotation(45)
ax2.plot(x_split_list, y_disagree_list)
plt.xticks(rotation=45)
plt.show()
print("日期", day_list)
print("情绪指数",y_mood_list)
print("分歧指数",y_disagree_list)
# 删除制定列
del df["标题"]
# 获取某一列的值
data = df.ix[:, ['发帖内容']].values
# 统计发帖的次数
df.groupby('发帖内容')['发帖内容'].count()
# 获取长度
df['发帖内容'].str.len()
# 统计字体的长度
df.groupby('length').count()
# 转列表
len_df.index.tolist()
# 9、绘制句子长度及频次统计
# 绘制句子长度及出现频数统计图
# plt.bar(sent_length, sent_freq)
# plt.title("句子长度及出现频数统计图", fontproperties=my_font)
# plt.title("句子长度及出现频数统计图")
# plt.xlabel("句子长度")
# plt.ylabel("句子长度出现的频数")
# plt.show()
# plt.savefig("./句子长度及出现频数统计图.png")
# plt.close()
# 10、绘制累计的分布函数
# 绘制句子长度累积分布函数(CDF)
sent_pentage_list = [(count / sum(sent_freq)) for count in accumulate(sent_freq)]
#
# 绘制CDF
plt.plot(sent_length, sent_pentage_list)
#
# 寻找分位点为quantile的句子长度
quantile = 0.91
# print(list(sent_pentage_list))
for length, per in zip(sent_length, sent_pentage_list):
if round(per, 2) == quantile:
index = length
break
print("\n分位点为%s的句子长度:%d." % (quantile, index))
#
# # 绘制句子长度累积分布函数图
plt.plot(sent_length, sent_pentage_list)
plt.hlines(quantile, 0, index, colors="c", linestyles="dashed")
plt.vlines(index, 0, quantile, colors="c", linestyles="dashed")
plt.text(0, quantile, str(quantile))
plt.text(index, 0, str(index))
plt.title("句子长度累积分布函数图")
plt.xlabel("句子长度")
plt.ylabel("句子长度累积频率")