该matlab代码对应的matplotlib代码是什么
我试图摆脱 matlab 并使用 python + matplotlib 代替。然而,我还没有真正弄清楚 matplotlib 等价于 matlab 'handles' 的是什么。这里有一些 matlab 代码,我在其中返回句柄,以便我可以更改某些属性。使用 matplotlib 的这段代码的等价物是什么?我经常在 matlab 中使用句柄的“Tag”属性,并使用“findobj”。这也可以用 matplotlib 来完成吗?
% create figure and return figure handle
h = figure();
% add a plot and tag it so we can find the handle later
plot(1:10, 1:10, 'Tag', 'dummy')
% add a legend
my_legend = legend('a line')
% change figure name
set(h, 'name', 'myfigure')
% find current axes
my_axis = gca();
% change xlimits
set(my_axis, 'XLim', [0 5])
% find the plot object generated above and modify YData
set(findobj('Tag', 'dummy'), 'YData', repmat(10, 1, 10))
I'm trying to go away from matlab and use python + matplotlib instead. However, I haven't really figured out what the matplotlib equivalent of matlab 'handles' is. So here's some matlab code where I return the handles so that I can change certain properties. What is the exact equivalent of this code using matplotlib? I very often use the 'Tag' property of handles in matlab and use 'findobj' with it. Can this be done with matplotlib as well?
% create figure and return figure handle
h = figure();
% add a plot and tag it so we can find the handle later
plot(1:10, 1:10, 'Tag', 'dummy')
% add a legend
my_legend = legend('a line')
% change figure name
set(h, 'name', 'myfigure')
% find current axes
my_axis = gca();
% change xlimits
set(my_axis, 'XLim', [0 5])
% find the plot object generated above and modify YData
set(findobj('Tag', 'dummy'), 'YData', repmat(10, 1, 10))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个 findobj 方法也是 matplotlib :
请注意
plt.plot
中的gid
参数通常由 matplotlib 使用(仅)当后端设置为“svg”时。它使用gid
作为某些分组元素的id
属性(例如line2d
、patch
、text )。
There is a findobj method is matplotlib too:
Note that the
gid
parameter inplt.plot
is usually used by matplotlib (only) when the backend is set to 'svg'. It use thegid
as theid
attribute to some grouping elements (likeline2d
,patch
,text
).我没有使用过matlab,但我认为这就是你想要的
当然,这只是一个基本图,还有更多内容。尽管 this 找到您想要的图表并查看其源代码。
I have not used matlab but I think this is what you want
Of course, this is just a basic plot, there is more to it.Go though this to find the graph you want and view its source code.