gpt4 book ai didi

python - pandas 绘图 - 多个颜色条

转载 作者:太空宇宙 更新时间:2023-11-03 16:57:43 26 4
gpt4 key购买 nike

如何使用 pandas 散点图正确绘制 2 个颜色条?现在第一个颜色条已重复:

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);

最佳答案

您可以直接使用matplotlib函数:

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)

产生

enter image description here

关于python - pandas 绘图 - 多个颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35281482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com