簡體   English   中英

不同colors的繪圖數

[英]Plotting numbers of different colors

我有一個 dataframe 具有以下結構:

  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
______________________________________________________

我想 plot 它們的 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()

如果我指定一種顏色,數字顯示正確,但是當我嘗試為每個文本分配顏色時,它只顯示黑色而不顯示 res,我做錯了什么?

我還想添加一個帶有顏色和類型的圖例

像這樣的東西,但數字有不同的顏色 像這樣的東西,但數字不同 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()

在此處輸入圖像描述

或者取決於你需要什么:

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

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM