绘制美观的图像,主要靠积累,直接上代码:
import matplotlib.pyplot as plt
import numpy as np
# generate x values
x = np.random.randn(1000)
# random measurements, no correlation
y1 = np.random.randn(len(x))
# strong correlation
y2 = 1.2 + np.exp(x)
plt.figure(figsize=(16,9))
ax1 = plt.subplot(121)
plt.scatter(x, y1, color='indigo', alpha=0.9, edgecolors='white', label='no correl')
plt.xlabel('no correlation')
plt.grid(True)
plt.legend()
ax2 = plt.subplot(122, sharey=ax1, sharex=ax1)
plt.scatter(x, y2, color='green', alpha=0.3, edgecolors='grey', label='correl')
plt.xlabel('strong correlation')
plt.grid(True)
plt.legend()
plt.show()


本文通过Python的Matplotlib和NumPy库,演示了如何绘制两种不同关联性的散点图,一种显示无关联的数据分布,另一种展示强关联的数据趋势。代码中详细展示了数据生成、绘图设置及展示的方法。

3万+

被折叠的 条评论
为什么被折叠?



