基于第三个变量的带有颜色条的2个变量的散点图 [英] Scatter plot of 2 variables with colorbar based on third variable

查看:132
本文介绍了基于第三个变量的带有颜色条的2个变量的散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用变量x相对于另一个y进行绘制,并基于另一个变量z的值添加一个颜色图.

I am trying to plot with variable x with respect to another y and add a colormap based on the values of another variable z

所以情节应该与此相似

我的尝试

import numpy as np
import pandas as pd
import seaborn as sns
import random
import matplotlib.pyplot as plt
import matplotlib.cm as cm

x=np.random.randint(0,20,30)
y=np.random.randint(-5,5,30)
z=np.random.randint(-2,10,30)
df=pd.DataFrame(data={'A':x,'B':y,'C':z})

points = plt.scatter(df['A'],df['B'],cmap="jet")
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(df['C'])
plt.colorbar(points)
sns.lmplot('A', 'B', data=df, hue='C', fit_reg=False)

TypeError: You must first set_array for mappable

我正在混合使用matplotlib和seaborn,因为在seaborn中我不能使用颜色表'jet'但是欢迎获得相同图形的任何替代方法

I am mixing matplotlib and seaborn as in seaborn I can not use the colormap 'jet' But any alternative aproaches to get the same graph are welcome

推荐答案

如何解决(在scatter命令中使用c = df.C):

How about this (use c=df.C in the scatter command):

points = plt.scatter(df.A, df.B, c=df.C,cmap="jet", lw=0)
plt.colorbar(points)

这篇关于基于第三个变量的带有颜色条的2个变量的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆