【发布时间】:2016-04-17 02:37:21
【问题描述】:
我有一个包含 3 列的数据文件。前 2 列代表坐标,第三列是字符串值,例如 'foo'、'bar' 或 'ter'。
我想根据这个标签、不同的标记和颜色用 python 的 matplotlib 显示。示例:
- foo => 红圈
- 条形 => 绿色三角形
- ter => 黑色方块
到目前为止我所做的是:
import numpy as np
import matplotlib.pyplot as plt
coordData = np.genfromtxt("mydata.csv", usecols=(0,1), delimiter=",", dtype=None)
coordLabels = np.genfromtxt("mydata.csv", usecols=2, delimiter=",", dtype=None)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(coordData[:, 0], coordData[:, 1], c="r", marker="o")
plt.show()
如何根据coordLabels 值切换标记和颜色?
解决方案
根据建议我做了一些更改:
coordData = np.genfromtxt("mydata.csv", usecols=(0, 1), delimiter=",", dtype=None)
coordLabels = np.genfromtxt("mydata.csv", usecols=2, delimiter=",", dtype=None)
fig = plt.figure()
ax = fig.add_subplot(111)
uniqueVals = np.unique(coordLabels)
markers = ['^', 'o', '*']
colors = { '^' : 'r',
'o' : 'b',
'*' : 'g'}
for marker, val in zip(markers, uniqueVals):
toUse = coordLabels == val
ax.scatter(coordData[toUse,0], coordData[toUse,1], c = colors[marker], marker=marker)
plt.show()
【问题讨论】:
标签: python matplotlib