![](/img/trans.png)
[英]How to creat a cursor for a figure/window of matplotlib with python shell?
[英]How to change color figure dots of matplotlib cursor? in python
我有一个 csv 经度、纬度和标签的数据
Longitude Latitude
0 106.895231 -6.302275
1 106.900976 -6.285152
2 106.873755 -6.237447
3 106.894059 -6.238875
4 106.820816 -6.311941
.. ... ...
225 106.938847 -6.131683
226 106.937381 -6.109117
227 106.932118 -6.147447
228 106.958474 -6.155166
229 106.862266 -6.129799
和标签
0 TMII
1 Monumen Pancasila Sakti
2 Taman Simanjuntak
3 Mall Cipinang Indah
4 Kebun Binatang Ragunan
...
225 Not Categorized
226 Not Categorized
227 Not Categorized
228 Not Categorized
229 Not Categorized
Name: Wisata, Length: 230, dtype: object
然后我有一个 matplotlib,它显示了一个带有下面鳕鱼的光标图形
X, Y, labels = df['Latitude'], df['Longitude'], df['Wisata']
Total = df['Wisata'].sum()
fig, ax = plt.subplots()
line, = ax.plot(X, Y, 'ro')
# for color in ['tab:red','tab:green','tab:blue','tab:purple','tab:forestgreen',
# 'tab:maroon','tab:sienna','tab:steelblue','tab:hotpink','tab:darkorchid',
# 'tab:navy','tab:orange','tab:lime','tab:black','tab:turquoise',
# 'tab:salmon','tab:magenta','tab:gold','tab:brown','tab:grey']:
# n = Total
# x, y = np.random.rand(2, n)
# scale = 200.0 * np.random.rand(n)
# ax.scatter(x, y, c=color, s=scale, label=Total,
# alpha=0.3, edgecolors='none')
# ax.legend()
# ax.grid(True)
#plt.scatter(X, y, c=labels, cmap=plt.colors.ListedColormap(mcolors))
mpl.cursor(ax).connect(
"add", lambda sel: sel.annotation.set_text(labels[sel.index]))
plt.show()
我想根据我拥有的不同标签为每个点赋予不同的颜色(目前有 20 种不同的标签)。
有关正确方法的任何建议吗?
根据这个答案...您需要为每个标签指定一种颜色。 您没有提供所有标签,但它可能看起来像这样:
# Make sure you have a colour for each label
colours = {'TMII':'red', 'Monumen Pancasila Sakti':'green', 'Taman Simanjuntak':'blue', 'Mall Cipinang Indah':'purple', 'Kebun Binatang Ragunan':'orange'}
ax.scatter(X, Y, c=labels.map(colours))
plot.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.