import numpy as np from PIL import Image import pandas as pd import matplotlib.pyplot as plt # 第一个 # data = 2*np.random.rand(10000, 2) - 1 # # 原本data∈[0,1]想办法改变值域 # # 现在data∈[-1,1] # x = data[:, 0] # y = data[:, 1] # idx = x**2 + y**2 < 1 # hole = x**2 + y**2 < 0.25 # idx = np.logical_and(idx, ~hole) # plt.plot(x[idx], y[idx], 'go', markersize=1) # plt.show() # 第二个 # p = np.random.rand(10000) # np.set_printoptions(edgeitems=5000, suppress=True) # plt.hist(p, bins=20, color='g', edgecolor='k') # plt.show() # 第三个 # N = 10000 # times = 100 # z = np.zeros(N) # for i in range(times): # z += np.random.rand(N) # z /= times # plt.hist(z, bins=20, color='m', edgecolor='k') # plt.show() # 第四个 # d = np.random.rand(30, 4) # print(d) # print(type(d)) # data = pd.DataFrame(data=d, columns=list('abcd')) # print('='*50) # print(data) # print(type(data)) # print(data[list('ab')]) # data.to_csv('data.csv', index=False, header=True) # print('文件保存成功.') # 第五个 # [-4, 2]=[0,1]*6-4 # d = 6*np.random.rand(10000) - 4 # plt.plot(d, 'r.') # plt.show() # 第六个 # x = np.linspace(0, 1, 100) # print(x) # y = x**x # plt.plot(x, y, 'r-', linewidth=3) # plt.show() # 第七个 # a = np.arange(1, 10000) # print(np.sqrt(6*np.sum(1 / (a**2)))) # 第八个 # image_file = 'lena.png' # height = 100 # img = Image.open(image_file) # img_width, img_height = img.size # width = int(1.8 * height * img_width // img_height) # img = img.resize((width, height), Image.ANTIALIAS) # pixels = np.array(img.convert('L')) # print('type(pixels) = ', type(pixels)) # print(pixels.shape) # print(pixels) # chars = "MNHQ$OC?7>!:-;." # N = len(chars) # step = 256 // N # print(N) # result = '' # for i in range(height): # for j in range(width): # result += chars[pixels[i][j] // step] # result += '\n' # with open('text.txt', mode='w') as f: # f.write(result)
标签:plt,img,没用,print,2022,np,Dec,data,pixels From: https://www.cnblogs.com/leekaihyun/p/17053731.html