Scatter plot of 2 variables with colorbar based on third variable [duplicate]

只愿长相守 提交于 2019-12-11 07:09:08

问题


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

So the plot should be similar to this

My try

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

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


回答1:


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)



来源:https://stackoverflow.com/questions/50527658/scatter-plot-of-2-variables-with-colorbar-based-on-third-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!