Python数据可视化常用函数记录

画图类

1..plt.bar(x,y,label='Runningtime',color='cyan') #方差图

2.plt.legend()#标签

3.plt.xlabel('number',fontsize=12)

   plt.ylabel('value') #横纵坐标标签

4.plt.savefig('E:/WULAB/Paper_of_picture/three_method_hengxiangduibi.png', dpi=600, bbox_inches = 'tight')#保存图片

5.plt.xticks([1.5,3.5,5.5,7.5,9.5,11.5],['Pure','Range','Confined','Confined+refine','Inspire+refine','Inspire+range+refine'],rotation = 45)#坐标重命名

6.保存出现空白图,将plt.show()放在保存语句后

7.颜色 代码bule red...

https://ask.qcloudimg.com/http-save/yehe-2398817/ieqwng00ww.png

8.灰度图转二值图像

ret,binary = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY)

9.多图组合

plt.subplot(x,y,z) #多图组合 x多少行,y多少列,z当前是第几个图  

10.散点图

plt.scatter(x, y, alpha=0.8)

11.坐标范围

plt.ylim((0, 1000))

12.关于cv2的错误,请先检查路径中是否存在中文

 13.隐藏坐标轴

ax1.get_yaxis().set_visible(False)

14.plt坐标轴是汉字,竖向排列,离轴距离

plt.rcParams['font.sans-serif'] = ['SimHei']
Ylabe1="\n".join(("源",'汇',"比"))
plt.ylabel(Ylabe1,rotation='horizontal',size=18,verticalalignment='center',labelpad=12)

15.python 图片坐标轴需要有角标

plt.ylabel("Area($\mathregular{km^2}$)",font)

16.灰度图转彩色图


img1 = img1.astype(np.uint8)

#%%
def grayscale_to_color(image):
    # 创建颜色映射表
    colormap = cv2.applyColorMap(image, cv2.COLORMAP_JET)
    return colormap

# 读取灰度图像
gray_image = img1

# 将灰度图转换为彩色图像
color_image = grayscale_to_color(gray_image)

# 显示结果
cv2.imshow("Colored Image", color_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Python opencv cv2.applyColorMap 伪色彩函数_hxxjxw的博客-CSDN博客

17.python中调整xticks与x轴的距离

    plt.xticks(fontproperties = 'Times New Roman', size = 35)
    plt.tick_params(axis='x', which='both', pad=10)

18.画函数等值线

plt.figure(figsize=(15,5))
plt.subplot(1,2,1)
x = np.linspace(-3, 3, 400)
y = np.linspace(-3, 3, 400)
X, Y = np.meshgrid(x, y)
Z = 100*(Y-X**2)**2+(1-X)**2
# 绘制等值线图
plt.contour(X, Y, Z, levels=np.logspace(0,7,35),cmap=plt.cm.jet,vmin=0,vmax=200,linewidth=1)
plt.colorbar()

19.plt画图汉字不能显示并考虑负号的情况

from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False

20.plt在画布任意位置添加文字

plt.annotate('最速下降法', xy=(0.42, 0), xycoords='figure fraction', fontsize=20, ha='center')

21.调整图像子图的宽高比例:aspect参数

axs[0].imshow(img2_, cmap='gray',aspect=aspect_ratio)

22.plt画数据统计图时,bar scatter调整图层 避免覆盖

plt.scatter(x,y,zorder = 1)

#zorder越大,出现的越靠前

Python库问题

1.pip list #查看版本号

2.pip install plotly(库名) -i http://pypi.douban.com/simple --trusted-host pypi.douban.com 安装库避免失败

3.python下载库的位置/路径(对源码更改)

路径  anaconda/Lib/site-packages

其他编码

1.两种等差数列np.linspace(1,12,3) 最后一个参数分割数量

np.arange(1,12,3) 最后一个参数 步长

2.多种数组的创建

​np.zeros()
A_and_phi=np.array([[0,0]]*k, dtype="float")

3.数据写入csv

import csv
f = open('RHT_L.csv','w',encoding='utf-8',newline='')
csv_writer = csv.writer(f)
csv_writer.writerow(["A","phi","depth"])
for i in range(num):
    csv_writer.writerow(L[i,:])
f.close()

4.写入txt

f = open('相似度_right.txt','w',encoding='utf-8',newline='')
for i in range(1,num-1):  
    x[1]=fnew[i]
    pcm = similaritymeasures.pcm(x, y)
    print(pcm)
    f.write(str(xticks[i-1])+':\t'+str(pcm)+'\n')
f.close()

plt.legend(loc='lower center', bbox_to_anchor=(1.1,-2),ncol=2, markerscale=2,fancybox=True,shadow=True,fontsize=12)

5.最大值所对应的索引

x = np.unravel_index(np.argmax(GQI),GQI.shape) 

6.随机生成数,基于不同的概率

def rand_pick(seq , probabilities):
    x = random.uniform(0 ,1)
    cumprob = 0.0
    for item , item_pro in zip(seq , probabilities):
        cumprob += item_pro
        if x < cumprob:
            break
    return item

7.文件排序--避免文件名中110排在20前,按序比较大小

files.sort(key = lambda x: int(x[25:-4]))#去除前后字母干扰

8.dataframe取特定行

df.loc[行数]

9.list 多维[[x,y],[1,0]] 去除重复元素

result = list(set(map(tuple, result)))
result = [list(i) for i in result]

10.数组的定义

A_and_phi=np.array([[0,0]]*k, dtype="float")

11.读取csv后,按照某一列排序,并且重新排序索引

    pic1_data = pic1_data.sort_values(2)     #按照第二列排序
    pic1_data = pic1_data.reset_index(drop=True)  

12.清华源安装包

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xxx

13.plt 循环里的label /legend只出现一次

for i in range(len(t_plot)):
    particles_x = z_plot[0][i]+well_positions[0][0]-well_positions[1][0]+np.random.randn()/10
    particles_y = z_plot[1][i]+well_positions[0][1]-well_positions[1][1]+np.random.randn()/10
    plt.scatter(particles_x, particles_y, color='blue', s=5, alpha=0.3,label="Injected Water" if i == 0 else "")

14.二维数组的数据(本是浮点数)转换为整数类型

img2 = img2.astype(np.uint8)

15.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor

检查图像的读取路径不含中文

二.数学知识类

DFT

人工智能-离散傅里叶变换Python代码实现 - 知乎
 

STFT

scipy.signal.stft函数官方文档:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.stft.html
窗函数类型(scipy.signal.get_window)官方文档:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.get_window.html
————————————————
版权声明:本文为CSDN博主「DeepThinkerr」的原创文章,遵循CC 4.0 BY-SA版权协议,转载
原文链接:https://blog.csdn.net/fate_mt/article/details/104728797/

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修神成仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值