简体   繁体   中英

How to change color figure dots of matplotlib cursor? in python

i have a data of csv Longitude,latitude and labels

      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

and labels

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

then i have a matplotlib that shows a cursor figure with the cod below

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()

现在输出 I want to give every dots different color based on different labels that i have (currently there is 20 different labels).

Any suggestions of correct way to do that?

As per this answer ... For every label you need to designate a colour. You have not provided all of your labels, but it could look something like this:

# 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM