如何为X轴上的刻度线设置动画? [英] How do I animate the ticks on the x-axis?

查看:51
本文介绍了如何为X轴上的刻度线设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 matplotlib axes 实例,我在其中通过 blitting 为 AxesImage 设置动画.

I have a matplotlib axes instance inside which I'm animating an AxesImage via blitting.

我想做的是在 x 轴上设置刻度的动画.我经常更新 AxesImage(以及随后)绘制其艺术家的数据,并且在每次更新时,我想移动一个额外的刻度以突出显示某物的位置.这就是我现在正在做的事情:

What I'd like to do is animate the ticks on the x-axis as well. I am updating the data on the AxesImage (and subsequently) drawing its artist quite frequently, and on each update I'd like to move one extra tick placed to highlight the position of something. This is what I'm doing right now:

axis = axes.get_xaxis
im.set_data(new_data)
axis.set_ticks([10,20,30,x,t])
axis.set_ticklabels(["p", "u", "z", "z", "i"])
axes.draw_artist(im)
axes.draw_artist(axis)

虽然我看到刻度正确更新,但标签没有.我认为轴 bbox 不包括轴,这可能吗?如果是这样,我该如何对其进行动画处理?我应该从其他地方复制并还原吗?

While I see the ticks correctly updating, the labels are not. I think that the axes bbox does not include the axes, is this possible? If so, how can I animate it? Should I copy and restore from somewhere else?

推荐答案

轴 bbox 不包括轴内部"之外的任何内容(例如,它不包括刻度标签、标题等)

The axes bbox doesn't include anything outside of the "inside" of the axes (e.g. it doesn't include the tick labels, title, etc.)

解决此问题的一种快速方法是在画图时仅抓住图形的整个区域.(例如 background = canvas.copy_from_bbox(fig.bbox))

One quick way around this is to just grab the entire region of the figure when you're blitting. (E.g. background = canvas.copy_from_bbox(fig.bbox))

如果您有多个子图并且只想为其中一个制作动画,这可能会导致问题.在这种情况下,您可以按照 background = canvas.copy_from_bbox(ax.bbox.expanded(1.1, 1.2)) 的方式做一些事情.不过,您必须估算所需的比率.

This can cause problems if you have multiple subplots and only want to animate one of them. In that case, you can do something along the lines of background = canvas.copy_from_bbox(ax.bbox.expanded(1.1, 1.2)). You'll have to guesstimate the ratios you need, though.

如果您需要刻度标签的确切范围,那就有点棘手了.最简单的方法是遍历ticklabel对象,并使用 ax.bbox 获得并集.您可以将其设置为单一格式: ax.bbox.union([ax.get_xticklabels()中用于标签的label.get_window_extent()).

If you need the exact extent of the tick labels, it's a bit trickier. The easiest way is to iterate through the ticklabel objects and get the union with ax.bbox. You can make this a one-liner: ax.bbox.union([label.get_window_extent() for label in ax.get_xticklabels()]).

无论如何,我认为这三个选项之一应该可以满足您的需求.

At any rate, one of those three options should do what you need, I think.

这篇关于如何为X轴上的刻度线设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆