下面是一个使用Python的matplotlib库来实现“按照条件填充格点散点图”的示例代码:
import matplotlib.pyplot as plt
import numpy as np
# 生成随机数据
np.random.seed(0)
n = 100
x = np.random.rand(n)
y = np.random.rand(n)
colors = np.random.rand(n)
sizes = np.random.randint(50, 300, n)
# 定义条件
condition = (x > 0.5) & (y > 0.5)
filtered_x = x[condition]
filtered_y = y[condition]
filtered_colors = colors[condition]
filtered_sizes = sizes[condition]
# 绘制散点图
fig, ax = plt.subplots()
scatter = ax.scatter(filtered_x, filtered_y, c=filtered_colors, s=filtered_sizes, cmap='coolwarm')
# 添加颜色条
cbar = plt.colorbar(scatter)
cbar.set_label('Color')
# 添加标题和标签
plt.title('Scatter Plot with Condition')
plt.xlabel('X')
plt.ylabel('Y')
# 显示图形
plt.show()
在这个示例代码中,我们首先生成了100个随机数据点,并设定了它们的颜色和大小。然后,我们定义了一个条件,即筛选出x和y坐标都大于0.5的数据点。根据这个条件,我们筛选出满足条件的数据并将它们绘制成散点图。最后,我们添加了颜色条、标题和标签,并显示了图形。
这个示例代码可以根据具体的条件和数据进行修改,以满足不同的需求。
上一篇:按照条件删除重复项