1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import matplotlib.pyplot as plt import numpy as np
plt.style.use('_mpl-gallery-nogrid')
n=100 Q=1 X, Y = np.meshgrid( np.linspace(1, n, n), np.linspace(1, n, n))
Z=np.ones((n,n)) Z=Z*Q i=0 while i!=100000: a=np.random.randint(0,n) b=np.random.randint(0,n) if Z[a][b]>=1: Z[a][b]-=1 c=np.random.randint(0,n) d=np.random.randint(0,n) Z[c][d]+=1 i=i+1
z=12 j=np.linspace(0,z,z+1) k=np.linspace(0,0,z+1) m=0 while m!=z+1: k[m]=np.sum(Z==m) m+=1
print(k)
fig, ax = plt.subplots(1, 2)
pcm=ax[0].pcolormesh(X, Y, Z) fig.colorbar(pcm, ax=ax[0], label='Interactive colorbar')
ax[1].bar(j, k, width=1, edgecolor="white", linewidth=0.7) plt.show()
|