简体   繁体   English

根据 matplotlib 设置散点的颜色

[英]Setting color of scatter according to the in matplotlib

Why are all points hotpink, please?为什么所有的点都是粉红色的? Where is a mistake.哪里出错了。 The desired result is that each point is with different colour according to the first column.期望的结果是每个点根据第一列具有不同的颜色。 Thanks谢谢

file:文件:

fer00001.txt     -30.5598   1  51183.7316 0.0    0.88884
her00034.txt     -12.9113   1  50124.7613 0.0    0.93370
occ00043.txt     -37.9350   1  50094.5721 0.0    0.94562
omc15564.txt     -9.53485   1  51576.4297 0.0    0.56777

Code:代码:

import numpy as np  
import matplotlib.pyplot as plt
import pandas as pd

BVS, RV = np.loadtxt('file', unpack=True, usecols=[1, 5])
sp = np.loadtxt('file', unpack=True, dtype='str', usecols=[0])

kratke = [w[:3] for w in sp]

fig, ax = plt.subplots(figsize=[10,6.5])

string = [i.replace(',', ', ').replace('fer', 'FEROS').replace('her', 'HEROS').replace('occ', 'CES').replace('omc', 'RETICON') for i in kratke]

d = {'BVS': BVS, 'RV': RV, 'sp': string}
df = pd.DataFrame(data=d)

colors = {'FEROS': 'purple', 'HEROS': 'blue', 'CES': 'green', 'RETICON' : 'hotpink'}

for i in np.unique(df['sp']):
    color = colors[i]
    df1 = df[df['sp'] == i] 
    ax.scatter(df['BVS'], df['RV'], color=color, marker='o', s=6, label=i)

plt.show()

EDIT after advice在建议后编辑

import numpy as np  
import matplotlib.pyplot as plt
import pandas as pd

BVS, RV = np.loadtxt('file', unpack=True, usecols=[1, 5])
sp = np.loadtxt('file', unpack=True, dtype='str', usecols=[0])

kratke = [w[:3] for w in sp]

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=[10,6.5])

string = [i.replace(',', ', ').replace('fer', 'FEROS').replace('her', 'HEROS').replace('occ', 'CES').replace('omc', 'RETICON') for i in kratke]

d = {'BVS': BVS, 'RV': RV, 'sp': string}
df = pd.DataFrame(data=d)

colors = {'FEROS': 'purple', 'HEROS': 'blue', 'CES': 'green', 'RETICON' : 'hotpink'}

for i in np.unique(df['sp']):
    color = colors[i]
    df1 = df[df['sp'] == i] 
    ax1.scatter(df1['BVS'], df1['RV'], color=color, marker='o', s=6, label=i)
    ax2.scatter(df1['BVS'][0:2], df1['RV'][0:2], color=color, marker='o', s=6, label=i)
plt.show()

Why slider does not work, please?请问为什么滑块不起作用? All points are displayed again.再次显示所有点。 In my original data, not points are displayed then.在我的原始数据中,然后不显示点。

  • The main issue is ax.scatter(df['BVS'], df['RV'], color=color, marker='o', s=6, label=i) should be ax.scatter(df1['BVS'], df1['RV'], color=color, marker='o', s=6, label=i)主要问题是ax.scatter(df['BVS'], df['RV'], color=color, marker='o', s=6, label=i)应该是ax.scatter(df1['BVS'], df1['RV'], color=color, marker='o', s=6, label=i)
    • df was plotted instead of df1 df被绘制而不是df1
  • The same thing can be accomplished more easily with the following使用以下方法可以更轻松地完成相同的事情
import pandas as pd
import seaborn as sns  # high level API for matplotlib

# load the columns with pandas instead of numpy
df = pd.read_csv('file', sep='\\s+', header=None, usecols=[0, 1, 5])

# name the columns
df.columns = ['sp', 'BVS', 'RV']

# use only the first 3 values of sp
df['sp'] = df['sp'].str[:3]

# sorted list of values to map sp to
mappings = ['FEROS', 'HEROS', 'CES', 'RETICON']

# create a dict mapping the unique values in sp to mappings
mapped = dict(zip(sorted(df['sp'].unique()), mappings))

# map sp to the new values
df['sp'] = df['sp'].map(mapped)

# display(df)
        sp       BVS       RV
0    FEROS -30.55980  0.88884
1    HEROS -12.91130  0.93370
2      CES -37.93500  0.94562
3  RETICON  -9.53485  0.56777
p = sns.scatterplot(data=df, x='BVS', y='RV', hue='sp')
p.legend(bbox_to_anchor=(1.01, 1.02), loc='upper left')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM