First, create your own function.
myplot.py
import matplotlib.pyplot as plt
def myplot(x):
plt.plot(x, 'o-')
plt.show()
if __name__ == '__main__':
x = [i**2 for i in range(20)]
myplot(x)
I like to make the file name and the function name the same here.
Put this in your own function collection folder.
mymodule/
__init__.py
myplot.py
If you keep this file inside,
__init__.py
from myplot import myplot
You can import myplot from mymodule without stress.
test_myplot.py
from mymodule import myplot
x = [(-0.5)**i for i in range(20)]
myplot(x)
However, in this case, the executed test_myplot.py must be in the same folder as mymodule.
mymodule
test_myplot.py
Therefore, collect your own function collection in a specific folder.
/home/okadate/pyfiles/
mymodule
mymodule2
If you put test_myplot.py in a free position like this,
test_myplot.py
import sys; sys.path.append('/home/okadate/pyfiles')
from mymodule import myplot
x = [i/(5.0+i) for i in range(30)]
myplot(x)
You can use it!
It seems that there are other methods such as packaging. I would like to try it if it is easy. ..
Recommended Posts