import matplotlib.pyplot as plt
def map_col(col):
if col == 'y_odd':
mapped_col = 0
elif col == 'y_even':
mapped_col = 1
return mapped_col
x = [1, 4, 2, 7, 4, 9]
y = [4, 1, 3, 6, 8, 2]
c = ['y_even', 'y_odd', 'y_odd', 'y_even', 'y_even', 'y_even']
color = [map_col(col) for col in c]
plt.scatter(x, y, s=100, c=color, cmap="gnuplot")
plt.show()
就第三个变量给各个点上色是很简单的。你只需要把这个定性变量映射到可以表示颜色的东西上。例如,如果第三个变量在以下代码中是“c”:
有关可用颜色贴图的列表,请参见:
https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html
相关问题 更多 >
编程相关推荐