该matlab代码对应的matplotlib代码是什么

发布于 2024-12-06 09:59:30 字数 679 浏览 2 评论 0原文

我试图摆脱 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 技术交流群。

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

发布评论

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

评论(3

_畞蕅 2024-12-13 09:59:30

有一个 findobj 方法也是 matplotlib :

import matplotlib.pyplot as plt
import numpy as np

h = plt.figure()
plt.plot(range(1,11), range(1,11), gid='dummy')
my_legend = plt.legend(['a line'])
plt.title('myfigure')  # not sure if this is the same as set(h, 'name', 'myfigure')
my_axis = plt.gca()
my_axis.set_xlim(0,5)
for p in set(h.findobj(lambda x: x.get_gid()=='dummy')):
    p.set_ydata(np.ones(10)*10.0)
plt.show()

请注意plt.plot 中的 gid 参数通常由 matplotlib 使用(仅)当后端设置为“svg”时。它使用 gid 作为某些分组元素的 id 属性(例如 line2dpatchtext )。

There is a findobj method is matplotlib too:

import matplotlib.pyplot as plt
import numpy as np

h = plt.figure()
plt.plot(range(1,11), range(1,11), gid='dummy')
my_legend = plt.legend(['a line'])
plt.title('myfigure')  # not sure if this is the same as set(h, 'name', 'myfigure')
my_axis = plt.gca()
my_axis.set_xlim(0,5)
for p in set(h.findobj(lambda x: x.get_gid()=='dummy')):
    p.set_ydata(np.ones(10)*10.0)
plt.show()

Note that the gid parameter in plt.plot is usually used by matplotlib (only) when the backend is set to 'svg'. It use the gid as the id attribute to some grouping elements (like line2d, patch, text).

风吹过旳痕迹 2024-12-13 09:59:30

我没有使用过matlab,但我认为这就是你想要的

import matplotlib
import matplotlib.pyplot as plt

x = [1,3,4,5,6]
y = [1,9,16,25,36]
fig = plt.figure()
ax = fig.add_subplot(111)  # add a plot
ax.set_title('y = x^2')
line1, = ax.plot(x, y, 'o-')       #x1,y1 are lists(equal size)
line1.set_ydata(y2)                #Use this to modify Ydata
plt.show()

当然,这只是一个基本图,还有更多内容。尽管 this 找到您想要的图表并查看其源代码。

I have not used matlab but I think this is what you want

import matplotlib
import matplotlib.pyplot as plt

x = [1,3,4,5,6]
y = [1,9,16,25,36]
fig = plt.figure()
ax = fig.add_subplot(111)  # add a plot
ax.set_title('y = x^2')
line1, = ax.plot(x, y, 'o-')       #x1,y1 are lists(equal size)
line1.set_ydata(y2)                #Use this to modify Ydata
plt.show()

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.

滴情不沾 2024-12-13 09:59:30
# create figure and return figure handle
h = figure()

# add a plot but tagging like matlab is not available here. But you can
# set one of the attributes to find it later. url seems harmless to modify.
# plot() returns a list of Line2D instances which you can store in a variable  
p = plot(arange(1,11), arange(1,11), url='my_tag')

# add a legend
my_legend = legend(p,('a line',)) 
# you could also do 
# p = plot(arange(1,11), arange(1,11), label='a line', url='my_tag')
# legend()
# or
# p[0].set_label('a line')
# legend()

# change figure name: not sure what this is for.
# set(h, 'name', 'myfigure') 

# find current axes
my_axis = gca()
# change xlimits
my_axis.set_xlim(0, 5)
# You could compress the above two lines of code into:
# xlim(start, end)

# find the plot object generated above and modify YData
# findobj in matplotlib needs you to write a boolean function to 
# match selection criteria. 
# Here we use a lambda function to return only Line2D objects 
# with the url property set to 'my_tag'
q = h.findobj(lambda x: isinstance(x, Line2D) and x.get_url() == 'my_tag')

# findobj returns duplicate objects in the list. We can take the first entry.
q[0].set_ydata(ones(10)*10.0) 

# now refresh the figure
draw()
# create figure and return figure handle
h = figure()

# add a plot but tagging like matlab is not available here. But you can
# set one of the attributes to find it later. url seems harmless to modify.
# plot() returns a list of Line2D instances which you can store in a variable  
p = plot(arange(1,11), arange(1,11), url='my_tag')

# add a legend
my_legend = legend(p,('a line',)) 
# you could also do 
# p = plot(arange(1,11), arange(1,11), label='a line', url='my_tag')
# legend()
# or
# p[0].set_label('a line')
# legend()

# change figure name: not sure what this is for.
# set(h, 'name', 'myfigure') 

# find current axes
my_axis = gca()
# change xlimits
my_axis.set_xlim(0, 5)
# You could compress the above two lines of code into:
# xlim(start, end)

# find the plot object generated above and modify YData
# findobj in matplotlib needs you to write a boolean function to 
# match selection criteria. 
# Here we use a lambda function to return only Line2D objects 
# with the url property set to 'my_tag'
q = h.findobj(lambda x: isinstance(x, Line2D) and x.get_url() == 'my_tag')

# findobj returns duplicate objects in the list. We can take the first entry.
q[0].set_ydata(ones(10)*10.0) 

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