绘制不同颜色的数字

时间:2020-05-06 14:34:46

标签: python python-3.x matplotlib

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

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

如果我指定一种颜色,则数字会正确显示,但是当我尝试将颜色分配给每个文本时,它仅显示黑色而没有显示分辨率,这是什么意思?

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

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

1 个答案:

答案 0 :(得分:1)

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

enter image description here

或根据您的需要:

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

enter image description here

或根据您的需要:

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

enter image description here