[英]Plotting numbers of different colors
I have a dataframe with the next structure:我有一个 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
______________________________________________________
and I want to plot the numbers in their x,y corrdinate with their correspoding color in a scatterplot.我想 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()
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?如果我指定一种颜色,数字显示正确,但是当我尝试为每个文本分配颜色时,它只显示黑色而不显示 res,我做错了什么?
I also want to add a legend with the color and the type我还想添加一个带有颜色和类型的图例
Something like this, but with the numbers in different colors
像这样的东西,但数字不同 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.