Python 绘制不同颜色的数字

Python 绘制不同颜色的数字,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我有一个具有下一个结构的数据帧: x | y | color | type | count ___________________ _______________________________ 0 | 1 | black | type1 | 4 0 | 2 | black | type2 | 3 0 | 3 | red

我有一个具有下一个结构的数据帧:

  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
______________________________________________________
我想在散点图中,用它们对应的颜色,画出x,y中的数字

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()
如果我指定一种颜色,数字会正确显示,但是当我尝试为每个文本指定颜色时,它只显示黑色,而不显示分辨率,我做错了什么

我还想添加一个带有颜色和类型的图例

类似这样,但数字颜色不同

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

或者根据您的需要:

或者根据您的需要:

特别针对他的问题:
plt.scatter(data\u-graph.x,data\u-graph.y,c=data\u-graph.color)
但这样就可以正确地绘制点了吗?如果我想要文本而不是要点,我会怎么做?见我答案的最后一部分。考虑一下投票和接受我的意见。我涵盖了所有3种可能的情况。令人惊讶的是,您提供的代码为我显示了预期的结果……天哪,我写了一个颜色错误,并且由于某种原因没有错误消息。顺便说一句,我建议删除ax.scatter(x,y,alpha=0)`并使用
plt.xlim(data\u graph.x.min()-0.5,data\u graph.x.max()+0.5)
plt.ylim(…)
plt.show()之前的

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