基于来自 CSV 的输入字段使用 matplotlib.pyplot 的多色散点图
Multi color scatter plot using matplotlib.pyplot based on input field from CSV
我想要一个散点图,其中每个点都根据标签着色:
相同的标签应使用相同颜色的点绘制。
eg: tag: 'One', color : red
tag: 'Two', color : green
输入 CSV 文件:(第一列是标签)
One;0;0.2345;0.43543;
Two;0.2345;0;0.34563;
One;0.43543;0.34563;0;
绘图代码:
import csv
import numpy as np
import matplotlib.pyplot as plt
from sklearn import manifold
reader = csv.reader(open("data.csv", "r"), delimiter=';')
data = list(reader)
dists = []
tags = []
for d in data:
tags.append(d[0])
dists.append(map(float , d[1:-1]))
adist = np.array(dists)
amax = np.amax(adist)
adist /= amax
mds = manifold.MDS(n_components=2, dissimilarity="precomputed", random_state=10)
results = mds.fit(adist)
coords = results.embedding_
plt.subplots_adjust(bottom = 0.1)
plt.scatter(coords[:, 0], coords[:, 1], marker = 'o')
for label, x, y in zip(tags, coords[:, 0], coords[:, 1]):
plt.annotate(
label,
xy = (x, y), xytext = (-8, 8),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.2', fc = 'blue', alpha = 0.1),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
plt.show()
现在所有显示的点都是同一种颜色。
我希望每个点都具有不同的颜色,基于标签的字符串值。
可以从 python
中的地图中选择颜色
{one:red, two:green, three:yellow}
由于您不仅仅是在代码中绘图,我将建议一个对您的代码进行最少更改的解决方案,以便您可以轻松地合并它:
- 为颜色选择创建字典:
color_dict = {'One':'red', 'Two':'green', 'Three':'yellow'}
- 通过将
plt.scatter()
移动到 for
循环并将其更改为 plt.scatter(x, y, c=color_dict[label], marker = 'o')
来逐个绘制点
一个完整的更新示例及其结果:
import csv
import numpy as np
import matplotlib.pyplot as plt
from sklearn import manifold
reader = csv.reader(open("data.csv", "r"), delimiter=';')
data = list(reader)
dists = []
tags = []
for d in data:
tags.append(d[0])
dists.append(map(float , d[1:-1]))
adist = np.array(dists)
amax = np.amax(adist)
adist /= amax
mds = manifold.MDS(n_components=2, dissimilarity="precomputed", random_state=10)
results = mds.fit(adist)
coords = results.embedding_
color_dict = {'One':'red', 'Two':'green', 'Three':'yellow'}
plt.subplots_adjust(bottom = 0.1)
#plt.scatter(coords[:, 0], coords[:, 1], marker = 'o')
for label, x, y in zip(tags, coords[:, 0], coords[:, 1]):
plt.scatter(x, y, c=color_dict[label], marker = 'o')
plt.annotate(
label,
xy = (x, y), xytext = (-8, 8),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.2', fc = 'blue', alpha = 0.1),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
plt.show()
我想要一个散点图,其中每个点都根据标签着色: 相同的标签应使用相同颜色的点绘制。
eg: tag: 'One', color : red
tag: 'Two', color : green
输入 CSV 文件:(第一列是标签)
One;0;0.2345;0.43543;
Two;0.2345;0;0.34563;
One;0.43543;0.34563;0;
绘图代码:
import csv
import numpy as np
import matplotlib.pyplot as plt
from sklearn import manifold
reader = csv.reader(open("data.csv", "r"), delimiter=';')
data = list(reader)
dists = []
tags = []
for d in data:
tags.append(d[0])
dists.append(map(float , d[1:-1]))
adist = np.array(dists)
amax = np.amax(adist)
adist /= amax
mds = manifold.MDS(n_components=2, dissimilarity="precomputed", random_state=10)
results = mds.fit(adist)
coords = results.embedding_
plt.subplots_adjust(bottom = 0.1)
plt.scatter(coords[:, 0], coords[:, 1], marker = 'o')
for label, x, y in zip(tags, coords[:, 0], coords[:, 1]):
plt.annotate(
label,
xy = (x, y), xytext = (-8, 8),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.2', fc = 'blue', alpha = 0.1),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
plt.show()
现在所有显示的点都是同一种颜色。 我希望每个点都具有不同的颜色,基于标签的字符串值。 可以从 python
中的地图中选择颜色{one:red, two:green, three:yellow}
由于您不仅仅是在代码中绘图,我将建议一个对您的代码进行最少更改的解决方案,以便您可以轻松地合并它:
- 为颜色选择创建字典:
color_dict = {'One':'red', 'Two':'green', 'Three':'yellow'}
- 通过将
plt.scatter()
移动到for
循环并将其更改为plt.scatter(x, y, c=color_dict[label], marker = 'o')
来逐个绘制点
一个完整的更新示例及其结果:
import csv
import numpy as np
import matplotlib.pyplot as plt
from sklearn import manifold
reader = csv.reader(open("data.csv", "r"), delimiter=';')
data = list(reader)
dists = []
tags = []
for d in data:
tags.append(d[0])
dists.append(map(float , d[1:-1]))
adist = np.array(dists)
amax = np.amax(adist)
adist /= amax
mds = manifold.MDS(n_components=2, dissimilarity="precomputed", random_state=10)
results = mds.fit(adist)
coords = results.embedding_
color_dict = {'One':'red', 'Two':'green', 'Three':'yellow'}
plt.subplots_adjust(bottom = 0.1)
#plt.scatter(coords[:, 0], coords[:, 1], marker = 'o')
for label, x, y in zip(tags, coords[:, 0], coords[:, 1]):
plt.scatter(x, y, c=color_dict[label], marker = 'o')
plt.annotate(
label,
xy = (x, y), xytext = (-8, 8),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.2', fc = 'blue', alpha = 0.1),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
plt.show()