[英]Setting different color for each series in scatter plot on matplotlib
[英]Setting color of scatter according to the in matplotlib
為什么所有的點都是粉紅色的? 哪里出錯了。 期望的結果是每個點根據第一列具有不同的顏色。 謝謝
文件:
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
代碼:
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()
在建議后編輯
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()
請問為什么滑塊不起作用? 再次顯示所有點。 在我的原始數據中,然后不顯示點。
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
被繪制而不是df1
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.