denfromufa February 2016

pandas plot - multiple colorbars

How can I properly plot 2 colorbars with pandas scatter plots? Right now the first colorbar is duplicated:

https://gist.github.com/denfromufa/45c446690a69265d39dd

import numpy as np
import pandas as pd
df=pd.DataFrame(np.random.random([100,5]),columns='A B C D E'.split())
df.head()


%matplotlib inline
ax1=df.plot(kind='scatter',x='A',y='B',c='C',s=df.D*50,cmap='summer',linewidth=0,sharex=False);
df.plot(ax=ax1,kind='scatter',x='A',y='C',c='D',s=df.B*50,cmap='winter',linewidth=0,sharex=False);

Answers


David Zwicker February 2016

You can use the matplotlib functions directly:

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

df = pd.DataFrame(np.random.random([100,5]),columns='A B C D E'.split())

sc1 = plt.scatter(x=df['A'], y=df['B'], c=df['C'], s=50*df['D'], cmap='summer')
plt.colorbar(sc1)
sc2 = plt.scatter(x=df['A'], y=df['C'], c=df['D'], s=50*df['B'], cmap='winter')
plt.colorbar(sc2)

which produces

enter image description here

Post Status

Asked in February 2016
Viewed 2,521 times
Voted 7
Answered 1 times

Search




Leave an answer