import numpy as np
import matplotlib.pyplot as plt
# 随机数生成器的种子
np.random.seed(19680801)
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5) # 设置颜色及透明度
plt.title("Random scatter diagram") # 设置标题
plt.show()
运行截图:
2.示例二
(2)绘制柱状图填充不同颜色
import matplotlib.pyplot as plt
import numpy as np
x = np.array(['naruto', 'sasuke', 'sakura']) # x轴3个数据
y = np.array([18, 20, 16]) # y轴数值
plt.bar(x, y, color=['b', 'y', 'g']) # 填充不同颜色 b