【发布时间】:2011-03-28 17:17:19
【问题描述】:
有人知道是否可以将 xtick 标签包装在 matplotlib 中吗?现在我有以下代码(有点乱——已经破解了一段时间):
def plotResults(request, question_id):
responses = ResponseOption.objects.filter(question__id=question_id).order_by('order').annotate(response_num=Count('response'))
counts = []
labels = []
for response in responses:
counts.append(response.response_num)
labels.append(smart_truncate('$'+response.text+'$'))
N = len(labels)
labels = tuple(labels)
counts = tuple(counts)
ind = na.array(range(N))+0.5
width = .35
fig = Figure(facecolor='white',edgecolor='white')
ax = fig.add_subplot(1,1,1)
rects1 = ax.bar(ind, counts,linewidth=0)
ax.set_ylabel('$Count$')
ax.set_title('$Response Historgram$')
ax.set_xticks(ind+width)
ax.set_xticklabels(labels)
print mpl.matplotlib_fname()
canvas = FigureCanvas(fig)
response = HttpResponse(content_type='image/png')
canvas.print_png(response)
return response
生成这个情节:
如您所见,xticks 已被剔除。关于如何包装它们或使它们可读的任何想法?再次感谢!
PS:这是 Django 项目的一部分。我将绘图作为 png 图像返回——通常从各种视图中的 img 标签调用它们。
【问题讨论】:
-
rotation = 'vertical'可能是答案:stackoverflow.com/questions/1221108/… -
@~unutbu:
rotation可以是任意角度,见:matplotlib.sourceforge.net/api/… -
@Amro,谢谢。
xticks(rotation=45)可能看起来更好...... -
# 使用 matplotlib 包装条形图的条形标签
stackoverflow.com/a/70443275/17736138
标签: python django matlab matplotlib