python - 如何将条形图用作“温度计”?
问题描述
我创建了一个 Python 脚本来提取评论中的情绪。现在我想用三个步骤来表示一般情绪:消极、中立和积极。我想使用条形图,但只有一个条用作温度计(条形越高,评论越好),但我有点卡住了。
import matplotlib.pyplot as plt
import numpy as np
# Bar chart
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
emotion = ['Negative', 'Neutral', 'Positive']
percentage = [23,17,60]
width = 0.35
ax.bar(emotion,percentage)
ax.set_ylabel('Positivity')
ax.set_title('General emotion in comments')
# here it's wrong but I don't know how to perform what I want
ax.bar(1, percentage[0], width, color='r')
ax.bar(1, percentage[1], width, color='b')
ax.bar(1, percentage[2], width, color='g')
###############################################
ax.set_yticks(np.arange(-100, 100, 10))
plt.show()
在这种情况下,我硬编码了 23% 的负面评论、17% 的中性评论和 60% 的正面评论,但我不知道如何:
- 打印轴信息(这里是积极性)
- 使用 my
percentage
来绘制条形图,因为目前我有三种条形图,但我想要一种条形图
我要创建的条形图是这样的:
感谢您的帮助
[编辑]
好的,我更改了一些代码:
fig, ax = plt.subplots(figsize=(12, 6))
emotion = ['Negative', 'Neutral', 'Positive']
percentage = [-23,17,60]
plt.title('General emotion in comments')
plt.xlabel(' ')
plt.ylabel('Positivity')
plt.ylim([-100, 100])
ax.bar(2, percentage[2], width=0.35, color='g')
ax.bar(2, percentage[0], width=0.35, color='r')
plt.show()
解决方案
在我得到了所有帮助之后
x = 1
fig, ax = plt.subplots(figsize=(12, 6))
emotion = ['Negative', 'Neutral', 'Positive']
percentage = [-23,17,60]
plt.title('General emotion')
plt.ylabel('Positivity')
plt.ylim([-100, 100])
ax.set_xlim(0,3)
plt.tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)
ax.bar(x, percentage[2], width=0.35, color='g')
ax.bar(x, percentage[0], width=0.35, color='r')
plt.show()
推荐阅读
- unique-id - 为所有 Pod 全局分配每个 Pod 8 位唯一 ID
- node.js - 为 pdf 文件创建读取流以上传到 s3 存储桶
- spring-boot - 为什么使用 RedisTemplate 在 springboot 中抛出 NoSuchMethodError?
- r - 通过使用 R 选择上一行中的值来填充缺失值
- elasticsearch - Elasticsearch:为什么无法通过“term”查询找到但可以通过“match”查询找到?
- android - 如何将另一个片段添加到 ViewPager2 中?
- swift - Swift:使用 base64 编码的字符串填充错误
- angular - 如何为 tabPanel 定义静态索引
- oracle-apex - apex oracle 19.1 中的照片滑块
- rabbitmq - NestJS 微服务异常