python的下标从0开始 与matlab从1开始相区分。
python的数据切片是“前包括 后不包括”
python中写a[1:5] 实际上是取a中下标为“1-4”的元素 即实际中的第2~5个元素。
python的数据用[ ]选择数据下标 ( )用于函数调用 而matlab用( )选择数据下标。注意区分。
python中若对数组采用了( )调用 会出现如下错误
SyntaxError: can’t assign to function call.
python中使用.mat数据文件
matlab中保存
python中调用
from scipy import io
data io.loadmat(pathname data.mat )[ data ]
matlab中使用python的数据
python保存
io.savemat( filename.mat , { VariableNameInMatlab : VariableNameInPython})
matlab调用
Variable load([pathname, filename.mat ]).VariableNameInMatlab;
python一些数学函数的应用基于numpy库和math库
np.log10(…)
from math import sqrt
查看列表长度 len(list)
查看矩阵、数组维度 array.shape()
转到一维向量 x x.flatten()
range 创建一个整数列表 常用于for循环。常用格式 range(start, stop, step) 即从start开始 以step为步长 取值到stop。注意 stop这个值是取不到的start默认为0。
step默认为1, 可以为负数 此时stop应该小于start。但是 若没有定义step的值 它默认为1 同时输入stop小于start时 只能得到一个空列表 如果在for循环中出现这个问题 就会导致循环进不去
range不能对小数操作 如果有需要 可以用np.arange,用法如下
for diameter in np.arange(0.01,8,8./1024):
更多参考链接: link.
import matplotlib.pyplot as plt
1. 分辨率
plt.rcParams[ figure.dpi ] 600
2. 尺寸
plt.rcParams[ figure.figsize ] (3.5, 1.75) #对应生成的图片宽和高 单位 英寸
3. 画布个数
plt.figure就只有一个画布 重复在上面画 plt.figure()加个括号就会新建一个画布。
4. 保存图片
plt.savefig( 路径/ 图名.png , bbox_inches tight )
bbox_inches tight’是为了使坐标轴标签更紧凑
5. 中文标签显示
plt.rcParams[ font.sans-serif ] [ SimHei ] #用来正常显示中文标签图片内容设置
1. 连续曲线设置
plt.plot(x, y, -* , markersize 5, linewidth 2, label legend_char , color g , linestyle -- )
在plot里用label属性写了图例后 还是需要写一句plt.legend()才能正常显示图例
plot里的点的大小由markersize或ms控制
2. 离散曲线设置
plt.scatter(x,y,s 16.,color (0.,0.5,0.))
scatter里点的大小由s控制 并且scatter里s和plot()的markersize大小的平方相同 即[s] markersize**2 参考链接
3. 坐标轴设置
font { family : Times New Roman , #字体设置
weight : normal , #笔画深浅设置
size : wordsize, #字体大小设置
plt.xlabel( xlabel , font)
plt.ylabel( ylabel , font)
图例设置注意这里有个prop
plt.legend([ legend1 , legend2 , legend3 ], prop font)#$lambda$ plt.legend(loc best ,markerscale 2.,numpoints 2,scatterpoints 1,fontsize 12)
图例内容可以这样统一设置 也可以在plot里单独写 见本节1.
loc:图例拜访位置 ‘best’为自适应寻找最佳位置
markerscale 用于调节legend中点是实际图中点大小的markerscale倍 参考markerscale
numpoints与scatterpoints
numpoints参数做用于调整用线条画出的点 即用plot()画出的点 legend中的点的数目 而scatterpoints参数做用于调整用散点图画出的点 即scatter()画出的点 legend中的点的数目 参考legend中点数
6. 标题设置
plt.title( Impact of , font)
7. 刻度设置
fig, ax plt.subplots() #需要先把坐标轴属性ax取出来 plt.tick_params(labelsize 5) #刻度字体大小设置 labels ax.get_xticklabels() ax.get_yticklabels() #刻度字体类型设置 [label.set_fontname( Times New Roman ) for label in labels]
8. 散点形状、颜色、连续图用点强调
Marker_Set [ v , * , . , ] line_set [ - , -- , -. , -o , -* , - ]
颜色名称参考 matplotlib颜色表
系统默认颜色调用 默认颜色调用
也可以用RGB比例调色 color [1,0,0.1]
控制连续线条种类的字符串 连续线条种类
9. 添加网格
plt.grid()利用子坐标画局部放大图 4 其他常见问题 数组切片问题
Zdr Zdr[0, 0, [0, 4], :, :, :]
即取第一维的第一列 第二维的第一列 第三维的1~5列 第四、五、六维的全部数据
用变量temp对字典S_back的值赋值时 如果直接写S_back[n_diameter] temp 那后面修改temp的值时 S_back[n_diameter]的值会自动跟着变 因此这里需要用深拷贝 如红框中所示 这样写 后面修改temp的值就不会自动修改字典的值了。
python中较长的语句如果一行写不完可以用“”来连接多行语句 在(),{},[] 中无需用“”连接



