是否可以在python中将xticks的文本包装在matplotlib中?

发布于 2024-09-14 07:40:51 字数 1134 浏览 5 评论 0原文

有人知道是否可以将 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

生成这个图:

alt text

如您所见,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:

alt text

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:

alt text

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:

alt text

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:

alt text

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:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

著墨染雨君画夕 2024-09-21 07:40:51

也许可以尝试:

ax.set_xticklabels(labels, rotation=45)

感谢 Amro 指出 旋转 可以是任何程度

Perhaps try:

ax.set_xticklabels(labels, rotation=45)

Thanks to Amro for pointing out that rotation can be any degree.

旧时模样 2024-09-21 07:40:51

如果您想手动换行标签,可以在标签名称中插入“\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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文