实例1·随机漫步图(matplotlib下的设置图像尺寸,隐藏坐标轴)

    xiaoxiao2025-05-03  14

    from random import choice import matplotlib.pyplot as plt x_start = [0] y_start = [0] #生成坐标矩阵 while len(x_start)<10000: x_direction=choice([-1,1]) x_distance=choice([0,1,2,3,4]) x_step=x_direction*x_distance y_direction=choice([-1,1]) y_distance=choice([0,1,2,3,4]) y_step=y_direction*y_distance next_x=x_start[-1]+x_step next_y=y_start[-1]+y_step x_start.append(next_x) y_start.append(next_y) #设置窗口大小,像素 plt.figure(dpi=128,figsize=(5,3)) #绘制 plt.scatter(x_start,y_start,c=list(range(10000)),cmap=plt.cm.Blues,edgecolors='none',s=15) #突出起始与结束 plt.scatter(0,0,c='red',edgecolors='none',s=80) plt.scatter(x_start[-1],y_start[-1],c='orange',edgecolors='none',s=80)#edgecolors='none'去除点的黑色轮廓,只显示蓝色的点 #隐藏坐标轴 plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.title('random—choice') plt.savefig('C://Users/Administrator/Desktop/text3.png') plt.show()

    结果展示

    最新回复(0)