【发布时间】:2014-06-02 06:27:14
【问题描述】:
我有一些数据列表,这些数据表明对李克特问题的回答,范围从 1(非常不开心)到 5(非常开心)。我想创建一个绘图页面,将这些列表显示为倾斜的堆叠水平条形图。响应列表可以有不同的大小(例如,当有人选择不回答特定问题时)。这是数据的最小示例:
likert1 = [1.0, 2.0, 1.0, 2.0, 1.0, 3.0, 3.0, 4.0, 4.0, 1.0, 1.0]
likert2 = [5.0, 4.0, 5.0, 4.0, 5.0, 3.0]
我希望能够用类似的东西来绘制这个:
plot_many_likerts(likert1, likert2)
目前我已经编写了一个函数来迭代列表,并将每个列表作为自己的子图绘制在 matplotlib 中的共享图形上:
def plot_many_likerts(*lsts):
#get the figure and the list of axes for this plot
fig, axlst = plt.subplots(len(lsts), sharex=True)
for i in range(len(lsts)):
likert_horizontal_bar_list(lsts[i], axlst[i], xaxis=[1.0, 2.0, 3.0, 4.0, 5.0])
axlst[i].axis('off')
fig.show()
def likert_horizontal_bar_list(lst, ax, xaxis):
cnt = Counter(lst)
#del (cnt[None])
i = 0
colour_float = 0.00001
previous_right = 0
for key in sorted(xaxis):
ax.barh(bottom=0, width=cnt[key], height=0.4, left=previous_right, color=plt.cm.jet(colour_float),label=str(key))
i += 1
previous_right = previous_right + cnt[key]
colour_float = float(i) / float(len(xaxis))
这效果不错,并且可以创建具有相同代表性尺寸的堆叠条形图(例如,宽度共享共同的轴刻度)。这是一个屏幕截图:
What is currently Produced http://s7.postimg.org/vh0j816gn/figure_1.jpg
我想要的是让这两个图以数据集模式的中点为中心(数据集将具有相同的范围)。例如:
What I would like to see http://s29.postimg.org/z0qwv4ryr/figure_2.jpg
关于我如何做到这一点的建议?
【问题讨论】:
-
继续调整
left,第二组条形开始previous_right与您想要的任何值对齐。 -
我希望有一种更简单的方法可以做到这一点,因为这意味着我必须跟踪创建的每个柱的中点值。感觉我必须自己做太多的会计工作,
matplotlib应该为我处理这个问题。 -
每个人都解决了这个?它被称为发散堆积条形图。 R 对此有一个模块(HH > Likert)。我也想创造一些,但想避免重新发明轮子。
-
不,只是拼凑一些东西,直到我得到足够好的东西......
标签: python matplotlib plot