首页 > 解决方案 > 如何将条形图用作“温度计”?

问题描述

我创建了一个 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% 的正面评论,但我不知道如何:

我要创建的条形图是这样的:

在此处输入图像描述

感谢您的帮助

[编辑]

好的,我更改了一些代码:

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()

标签: pythonmatplotlibbar-chart

解决方案


在我得到了所有帮助之后

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()

推荐阅读