Is it possible to build in a custom function using python and matplotlib?

What I want to do is define a function that contains the construction of sentences. Like this:

import matplotlib.pyplot as plt def myfun(args, ax): #...do some calculation with args ax.plot(...) ax.axis(...) fig.plt.figure() ax1=fig.add_subplot(121) ax2=fig.add_subplot(122) para=[[args1,ax1],[args2,ax2]] map(myfun, para) 

I found that myfun is called. If I add plt.show () to myfun, it may appear in the correct subtitle, but nothing else. And, if plt.show () is added at the end, only two axis pairs are drawn. I think the problem is that this figure is not passed to the main function successfully. Is it possible to do something similar using python and matplotlib? Thanks!

+4
source share
1 answer

A function called through a card must have only one parameter.

 import matplotlib.pyplot as plt def myfun(args): data, ax = args ax.plot(*data) fig = plt.figure() ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122) para = [ [[[1,2,3],[1,2,3]],ax1], [[[1,2,3],[3,2,1]],ax2], ] map(myfun, para) plt.show() 

If you want to keep your function signature, use itertools.starmap .

 import itertools import matplotlib.pyplot as plt def myfun(data, ax): ax.plot(*data) fig = plt.figure() ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122) para = [ [[[1,2,3],[1,2,3]],ax1], [[[1,2,3],[3,2,1]],ax2], ] list(itertools.starmap(myfun, para)) # list is need to iterator to be consumed. plt.show() 
+5
source

Source: https://habr.com/ru/post/1486538/


All Articles