登录论坛

查看完整版本 : 是否可以在python的matplotlib中包装xticks的文本?


poster
2019-12-10, 20:48
有人知道是否可以将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 生成此图:

https://i.imgur.com/tj8Cm.png

如您所见,xticks是骨头。关于如何包装它们或使它们可读的裸露的任何想法?再次感谢!

PS:这是Django项目的一部分。我将绘图以png图像形式返回-通常在各种视图中从img标签调用它们。



回答:

也许尝试:

ax.set_xticklabels(labels, rotation=45) 感谢Amro指出rotation可以是任意角度 (http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.xticks) 。



更多&回答... (https://stackoverflow.com/questions/3464359)