在Python的matplotlib中,可以包裹xticks的文本吗?
有人知道在matplotlib中是否可以换行xtick标签吗?我现在有以下这段代码(有点乱——我已经折腾了一段时间了):
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标签调用它们。
2 个回答
13