Python散点图根据值绘制不同的颜色
我有一个数据框,我想做一个散点图 数据帧看起来像:Python散点图根据值绘制不同的颜色,python,pandas,matplotlib,plot,Python,Pandas,Matplotlib,Plot,我有一个数据框,我想做一个散点图 数据帧看起来像: year length Animation 0 1971 121 1 1 1939 71 1 2 1941 7 0 3 1996 70 1 4 1975 71 0 我希望散点图中的点具有不同的颜色,具体取决于动画行中的值。 所以动画=1=黄色 动画=0=黑色 或者类似
year length Animation
0 1971 121 1
1 1939 71 1
2 1941 7 0
3 1996 70 1
4 1975 71 0
我希望散点图中的点具有不同的颜色,具体取决于动画行中的值。所以动画=1=黄色
动画=0=黑色
或者类似的东西 我试着做了以下几点:
dfScat = df[['year','length', 'Animation']]
dfScat = dfScat.loc[dfScat.length < 200]
axScat = dfScat.plot(kind='scatter', x=0, y=1, alpha=1/15, c=2)
dfScat=df[['year','length','Animation']]
dfScat=dfScat.loc[dfScat.length<200]
axScat=dfScat.plot(kind='scatter',x=0,y=1,alpha=1/15,c=2)
这会产生一个滑块,很难区分两者之间的区别 使用
scatter
中的c
参数
df.plot.scatter('year', 'length', c='Animation', colormap='jet')
通过将数组传递给c,也可以为点指定离散颜色= 像这样:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
d = {"year" : (1971, 1939, 1941, 1996, 1975),
"length" : ( 121, 71, 7, 70, 71),
"Animation" : ( 1, 1, 0, 1, 0)}
df = pd.DataFrame(d)
print(df)
colors = np.where(df["Animation"]==1,'y','k')
df.plot.scatter(x="year",y="length",c=colors)
plt.show()
这使得:
Animation length year
0 1 121 1971
1 1 71 1939
2 0 7 1941
3 1 70 1996
4 0 71 1975
这正是我要做的。我只是用他们的索引而不是名字。祝你好运。但是我如何完全根据颜色的值来改变颜色呢。我没有介于0和1之间的任何内容。例如,滑块上的1=黄色和0=黑色。请尝试使用该列名称。使用不同的颜色贴图。干杯,我不知道颜色贴图。:)