+------------+------+------+---------+
| promo_name | in | out | model |
+------------+------+------+---------+
| A | 0,87 | 0,13 | plan |
+------------+------+------+---------+
| B | 0,77 | 0,23 | plan |
+------------+------+------+---------+
| C | 0,54 | 0,46 | plan |
+------------+------+------+---------+
| A | 0,59 | 0,41 | predict |
+------------+------+------+---------+
| B | 0,50 | 0,50 | predict |
+------------+------+------+---------+
| C | 0,46 | 0,54 | predict |
+------------+------+------+---------+
你好。我有一个数据表,如上表所示,我想用值绘制一个堆积的条形图,例如:
但是到目前为止,它只能像这样工作,我的代码如下:
ratio.set_index(['promo_name', 'model'], inplace=True)
ratio = ratio.reindex(index = [('A', 'plan'), ('A', 'predict'),
('B', 'plan'), ('B', 'predict'),
('C', 'plan'), ('C', 'predict')])
plt.rcParams["figure.figsize"] = (12,5)
ratio.plot(kind='bar', stacked=True)
plt.xticks(rotation=30)
plt.show()
但是,直到我弄错了事情,我该如何解决?
最佳答案
使用matplotlib,您可以轻松地试一下这些条的位置和宽度:
import matplotlib.pyplot as plt
# plan, predict
Aout = (0.87, 0.59)
Ain = (0.13, 0.41)
Bout = (0.77, 0.50)
Bin = (0.23, 0.50)
Cout = (0.54, 0.46)
Cin = (0.46, 0.54)
width = 1.0 # the width of the bars
A_positions = [0, 1] # Positions for A bars
p1A = plt.bar([0, 1], (1.0, 1.0), width, color='g', label='out')
p2A = plt.bar([0, 1], Ain, width, color='b', label='in')
B_positions = [3, 4] # Positions for B bars
p1B = plt.bar(B_positions, (1.0, 1.0), width, color='g')
p2B = plt.bar(B_positions, Bin, width, color='b')
C_positions = [6, 7] # Positions for C bars
p1C = plt.bar(C_positions, (1.0, 1.0), width, color='g')
p2C = plt.bar(C_positions, Cin, width, color='b')
positions = A_positions + B_positions + C_positions # All together for ticks
plt.xticks(positions, ('A (plan)', 'A (predict)', 'B (plan)', 'B (predict)', 'C (plan)', 'C (predict)'))
plt.xticks(rotation=70)
plt.yticks([1, 0])
plt.legend()
plt.tight_layout()
并添加文本标签(您可以在该位置玩耍):
plt.text(A_positions[0] - 0.25, Ain[0] / 2, str(Ain[0]))
plt.text(A_positions[0] - 0.25, Aout[0] / 2, str(Aout[0]))
plt.text(A_positions[1] - 0.25, Ain[1] / 2, str(Ain[1]))
plt.text(A_positions[1] - 0.25, Aout[1] / 2 + 0.5, str(Aout[1]))
plt.text(B_positions[0] - 0.25, Bin[0] / 2, str(Bin[0]))
plt.text(B_positions[0] - 0.25, Bout[0] / 2, str(Bout[0]))
plt.text(B_positions[1] - 0.25, Bin[1] / 2, str(Bin[1]))
plt.text(B_positions[1] - 0.25, Bout[1] / 2 + 0.5, str(Bout[1]))
plt.text(C_positions[0] - 0.25, Cin[0] / 2, str(Cin[0]))
plt.text(C_positions[0] - 0.25, Cout[0] / 2 + 0.5, str(Cout[0]))
plt.text(C_positions[1] - 0.25, Cin[1] / 2, str(Cin[1]))
plt.text(C_positions[1] - 0.25, Cout[1] / 2 + 0.5, str(Cout[1]))
关于python - 带值的堆积条,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58434263/