从文档中:
默认转换指定文本位于数据坐标中,或者,您可以指定轴坐标中的文本(0,0 为左下角,1,1 为右上角)。下面的示例将文本放置在轴的中心:
Run Code Online (Sandbox Code Playgroud)>>> text(0.5, 0.5, 'matplotlib', horizontalalignment='center', ... verticalalignment='center', transform=ax.transAxes)
我可以同时 使用数据和轴坐标吗?分别对于x和y。
示例代码:
>>> text(0.5, 0.5, 'matplotlib', horizontalalignment='center',
... verticalalignment='center', transform=ax.transAxes)
Run Code Online (Sandbox Code Playgroud)
另请参阅: 将文本放在 matplotlib 图的左上角
例:
#!/bin/bash
function my_test(){
echo this is a test $1
}
my_test 1
python -c "from subprocess import check_output; print(check_output('my_test 2', shell=True))"
Run Code Online (Sandbox Code Playgroud)
输出:
this is a test 1
/bin/sh: my_test: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.5/subprocess.py", line 629, in check_output
**kwargs).stdout
File "/usr/lib/python3.5/subprocess.py", line 711, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'my_test 2' returned non-zero exit status 127
Run Code Online (Sandbox Code Playgroud) 因此,我注意到,当我想移动文件'a'并覆盖目标位置'b'时,os.remove('b')然后os.rename('a','b')的运行速度比shutil快得多.move('a','b')。
我读过说:
如果目标位于当前文件系统上,则使用os.rename()。否则,将src复制(使用shutil.copy2())到dst,然后将其删除。对于符号链接,将在dst中或作为dst创建指向src目标的新符号链接,并删除src。
但是为什么它还不使用os.remove()?
示例(第一次使用timeit,如果有任何错误,请抱歉):
import os,timeit
os.chdir('c:\python')
def myMove(a,b):
os.remove(b)
os.rename(a,b)
with open('src1', 'wb') as fout:
fout.write(os.urandom(350000000))
with open('src2', 'wb') as fout:
fout.write(os.urandom(350000000))
with open('dest1', 'wb') as fout:
fout.write(os.urandom(350000000))
with open('dest2', 'wb') as fout:
fout.write(os.urandom(350000000))
print('shutil.move(): %.3f' %timeit.timeit('shutil.move(os.path.join("c:\python","src1"),os.path.join("c:\python","dest1"))','import shutil,os.path', number = 1))
print('os.rename(): %.3f' %timeit.timeit('myMove("src2","dest2")','from __main__ import myMove', number = 1))
Run Code Online (Sandbox Code Playgroud)
印刷品:
shutil.move():0.81
os.rename():0.052