是否可以在python中将xticks的文本包装在matplotlib中?
有人知道是否可以将 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 标签中调用它们。
Anyone know if it is possible to wrap the xtick labels in matplotlib? Right now I've got the following code (kind of messy -- been hacking at it for a while):
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('
That generates this plot:
As you can see the xticks are boned. Any ideas on how to wrap them, or baring that make them readable? Thanks again!
PS: This is part of a Django project. I return the plot as a png image -- normally call them from img tags in various views.
+response.text+'
That generates this plot:
As you can see the xticks are boned. Any ideas on how to wrap them, or baring that make them readable? Thanks again!
PS: This is part of a Django project. I return the plot as a png image -- normally call them from img tags in various views.
))
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
That generates this plot:
As you can see the xticks are boned. Any ideas on how to wrap them, or baring that make them readable? Thanks again!
PS: This is part of a Django project. I return the plot as a png image -- normally call them from img tags in various views.
)
ax.set_title('$Response Historgram
That generates this plot:
As you can see the xticks are boned. Any ideas on how to wrap them, or baring that make them readable? Thanks again!
PS: This is part of a Django project. I return the plot as a png image -- normally call them from img tags in various views.
)
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
That generates this plot:
As you can see the xticks are boned. Any ideas on how to wrap them, or baring that make them readable? Thanks again!
PS: This is part of a Django project. I return the plot as a png image -- normally call them from img tags in various views.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许可以尝试:
感谢 Amro 指出
旋转
可以是任何程度。Perhaps try:
Thanks to Amro for pointing out that
rotation
can be any degree.如果您想手动换行标签,可以在标签名称中插入“\n”,这会将标签在“\n”处分成两行。您可以在此处查看此示例。
似乎还有一个 autowrap 函数 现在似乎可以很好地实现这一点。此示例使用 plt.text,但您也可以使用 plt.xticks 指定它的属性。 (即
wrap=True
)我发现这种标签的对齐方式很混乱,所以我还需要调整水平和垂直对齐方式。If you want to wrap the labels manually you can insert a '\n' into the label name, which will break the label into two lines at the point you have the '\n'. You can see an example of this here.
There also appears to be an autowrap function now that seems to do the trick nicely. This example uses plt.text, but it's an attribute you can specify with plt.xticks, too. (i.e.
wrap=True
) I found this kind of messed up the alignment of the labels, so I needed to tweak the horizontal and vertical alignments as well.