简体   繁体   中英

Plotting numbers of different colors

I have a dataframe with the next structure:

  x    |      y     |  color     |   type  | count 
___________________ _______________________________  
 0     |     1    |   black      | type1   |  4
 0     |     2    |   black      | type2   |  3
 0     |     3    |   red        | type3   |  7
 0     |     4    |  yellow      | type4   |  4
 1     |     1    |  green       | type5   |  8
______________________________________________________

and I want to plot the numbers in their x,y corrdinate with their correspoding color in a scatterplot.

import matplotlib.pyplot as plt

f = plt.figure(figsize=(5,5), dpi=120)
ax = f.add_subplot(111)

for i in range(len(data_graph)):
    x = data_graph.loc[i,'x']
    y = data_graph.loc[i,'y']
    c = str(data_graph.loc[i,'color'])
    print(c)
    t = str(data_graph.loc[i,'count'])
    ax.text(x,y,t, ha="center", va="center",color=c)
    ax.scatter(x,y, alpha=0)

plt.show()

If i specify a single color the numbers appear correctly, but when i try to assign the color to each text it shows only the black and doesn't show the res, what am I doing wrong?

I also want to add a legend with the color and the type

像这样的东西,但数字有不同的颜色 Something like this, but with the numbers in different colors

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0,0,0,0,1]) # x = data_graph.x.values
y = np.array([1,2,3,4,1]) # y = data_graph.y.values
color = np.array(['black', 'black', 'red', 'yellow', 'green']) # color = data_graph.color.values
types = np.array(['type1','type2','type3','type4','type5']) # types = data_graph.type.values

for i in range(np.unique(color).shape[0]):
    x_plot = x[color== np.unique(color)[i]]
    y_plot = y[color== np.unique(color)[i]]
    c = np.unique(color)[i]
    label = np.unique(color)[i] +'_' + types[i]
    plt.scatter(x_plot,y_plot, c = c, label=label)

plt.legend()
plt.show()

在此处输入图像描述

Or depending on what you need:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0,0,0,0,1]) # x = data_graph.x.values
y = np.array([1,2,3,4,1]) # y = data_graph.y.values
color = np.array(['black', 'black', 'red', 'yellow', 'green']) # color = data_graph.color.values
types = np.array(['type1','type2','type3','type4','type5'])

for i in range(np.unique(types).shape[0]):
    x_plot = x[types== np.unique(types)[i]]
    y_plot = y[types== np.unique(types)[i]]
    c = color[types==types[i]][0]
    label = c +'_' + types[i]
    plt.scatter(x_plot,y_plot, c = c, label=label)

plt.legend()
plt.show()

在此处输入图像描述

Or depending on what you need:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0,0,0,0,1]) # x = data_graph.x.values
y = np.array([1,2,3,4,1]) # y = data_graph.y.values
color = np.array(['black', 'black', 'red', 'yellow', 'green']) # color = data_graph.color.values
types = np.array(['type1','type2','type3','type4','type5'])

texts = np.array([20,30,40,50,60])

for i in range(np.unique(types).shape[0]):
    x_plot = x[types== np.unique(types)[i]]
    y_plot = y[types== np.unique(types)[i]]
    c = color[types==types[i]][0]
    label = c +'_' + types[i]
    plt.scatter(x_plot,y_plot, c = c, label=label)

for i, txt in enumerate(texts):
    plt.annotate(txt, (x[i], y[i]))

plt.legend()
plt.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