python - Passing string as an argument for plotting -


i function take string, indicating data(x, y or z) should plot, argument.

def plotfit(axis):      fig = plt.figure()     ax = fig.gca(projection='3d')     ax.set_xlabel('x')     ax.set_ylabel('y')     ax.set_zlabel('z')     ax.scatter(xdata, ydata, func_z(vars,fitted_z),c='r')     ax.set_title('b_z of %s' % name)     ax.scatter(xdata, ydata, zfield, c='b') 

how make bolded parts of code below replaced string argument that, e.g. plotfit(x) replace instances of bolded z below "x" , plot accordingly ? points of interest:

  • func_z
  • fitted_z
  • zfield
  • 'b_z of %s'

what imagine along lines of:

ax.scatter(xdata, ydata, func(axis string)(vars,fitted_(axis string)),c='r') 

one solution use exec execute different code depending on string type argument presume want parse function, example:

def plotfit(axis):     fig = plt.figure()     ax = fig.gca(projection='3d')     ax.set_xlabel('x')     ax.set_ylabel('y')     ax.set_zlabel('z')     exec("ax.scatter(xdata, ydata, func_" + axis + "(vars,fitted_" + axis + "),c='r')") 

a similar technique can used other lines want on.

please bear in mind not recommended use exec (or eval) can hide bugs , can ambiguous. see: why should exec() , eval() avoided?


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -