что является соответствующим кодом Matplotlib этого кода Matlab - PullRequest
2 голосов
/ 24 сентября 2011

Я пытаюсь уйти от matlab и использовать вместо него python + matplotlib. Тем не менее, я до сих пор не выяснил, что такое эквивалент ручек matlab для matplotlib. Вот код Matlab, в котором я возвращаю дескрипторы, чтобы изменить некоторые свойства. Что является точным эквивалентом этого кода с использованием matplotlib? Я очень часто использую свойство 'Tag' дескрипторов в matlab и использую '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))

Ответы [ 3 ]

4 голосов
/ 24 сентября 2011

Существует 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()

Обратите внимание, что параметр gid в plt.plot обычно используется matplotlib (только), когда бэкэнд установлен в 'svg'. Он использует gid в качестве атрибута id для некоторых элементов группировки (например, line2d, patch, text).

0 голосов
/ 24 сентября 2011
# 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()
0 голосов
/ 24 сентября 2011

Я не использовал 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()

Конечно, это просто базовый график, это еще не все. Впрочем, это , чтобы найти нужный график и просмотреть его исходный код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...