根据第三个变量更改散点图中的标记样式 [英] Changing marker style in scatter plot according to third variable
问题描述
我正在处理多列字典.我想绘制两列,然后根据第三列和第四列更改标记的颜色和样式.
I am dealing with a multi-column dictionary. I want to plot two columns and subsequently change color and style of the markers according to a third and fourth column.
我很难改变 pylab 散点图中的标记样式.不幸的是,我的方法适用于颜色,但不适用于标记样式.
I struggle with changing the marker style in the pylab scatter plot. My approach, which works for color, unfortunately does not work for marker style.
x=[1,2,3,4,5,6]
y=[1,3,4,5,6,7]
m=['k','l','l','k','j','l']
for i in xrange(len(m)):
m[i]=m[i].replace('j','o')
m[i]=m[i].replace('k','x')
m[i]=m[i].replace('l','+')
plt.scatter(x,y,marker=m)
plt.show()
推荐答案
问题在于 marker
只能是单个值而不是标记列表,如 color代码>参数.
The problem is that marker
can only be a single value and not a list of markers, as the color
parmeter.
您可以按标记值进行分组,以便您拥有具有相同标记的 x 和 y 列表并绘制它们:
You can either do a grouping by marker value so you have the x and y lists that have the same marker and plot them:
xs = [[1, 2, 3], [4, 5, 6]]
ys = [[1, 2, 3], [4, 5, 6]]
m = ['o', 'x']
for i in range(len(xs)):
plt.scatter(xs[i], ys[i], marker=m[i])
plt.show()
或者你可以绘制每个点(我不推荐):
Or you can plot every single dot (which I would not recommend):
x=[1,2,3,4,5,6]
y=[1,3,4,5,6,7]
m=['k','l','l','k','j','l']
mapping = {'j' : 'o', 'k': 'x', 'l': '+'}
for i in range(len(x)):
plt.scatter(x[i], y[i], marker=mapping[m[i]])
plt.show()
这篇关于根据第三个变量更改散点图中的标记样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!