【问题标题】:Plot data from two DataFrame with only one colorbar in a scatter plot绘制来自两个 DataFrame 的数据,散点图中只有一个颜色条
【发布时间】:2021-08-06 17:31:12
【问题描述】:

我有两个DataFrame 用于两个不同的数据集,其中包含RADecVel 列。我需要将它们绘制到相同的散点图并显示一个 colorbar 而不是两个。使用纯matplotlibhere 也有类似的问题,但我需要使用pandas 的散点图函数来完成。到目前为止,这是我的实验:

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

data1 = pd.DataFrame({'RA':np.random.randint(-100,100,5),
         'Dec':np.random.randint(-100,100,5),'Vel':np.random.randint(-20,10,5)})
data2 = pd.DataFrame({'RA':np.random.randint(-100,100,5),
         'Dec':np.random.randint(-100,100,5),'Vel':np.random.randint(-10,20,5)})
fig, ax = plt.subplots(figsize=(12, 10))


data1.plot.scatter(x='RA',y='Dec',c='Vel',cmap='rainbow',
            marker='^',ax=ax,label='Methanol',vmin=-20, vmax=20)
data2.plot.scatter(x='RA',y='Dec',c='Vel',cmap='rainbow',
            marker='o',ax=ax,label='Water',vmin=-20, vmax=20)

ax.set_xlabel('$\Delta$RA (arcsec.)')
ax.set_ylabel('$\Delta$Dec. (arcsec.)')
ax.set_title('Maser Spot')
ax.invert_xaxis()
ax.legend(loc=2)

使用此代码,我设法将两个DataFrame 绘制成一个散点图。但它显示了两个颜色条,如您在此处看到的: Test Case.

感谢任何帮助。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    您可以在第一个图中添加colorbar = False

    最终的代码是:

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    data1 = pd.DataFrame({'RA':np.random.randint(-100,100,5),
             'Dec':np.random.randint(-100,100,5),'Vel':np.random.randint(-20,10,5)})
    data2 = pd.DataFrame({'RA':np.random.randint(-100,100,5),
             'Dec':np.random.randint(-100,100,5),'Vel':np.random.randint(-10,20,5)})
    fig, ax = plt.subplots(figsize=(12, 10))
    
    
    data1.plot.scatter(x='RA',y='Dec',c='Vel',cmap='rainbow',
                marker='^',ax=ax,label='Methanol',vmin=-20, vmax=20,
                      colorbar=False)
    data2.plot.scatter(x='RA',y='Dec',c='Vel',cmap='rainbow',
                marker='o',ax=ax,label='Water',vmin=-20, vmax=20)
    
    ax.set_xlabel('$\Delta$RA (arcsec.)')
    ax.set_ylabel('$\Delta$Dec. (arcsec.)')
    ax.set_title('Maser Spot')
    ax.invert_xaxis()
    ax.legend(loc=2)
    

    【讨论】:

    • 啊,是的,感谢您指出这一点。我没有意识到他们已经处于相同的规模。
    猜你喜欢
    • 2019-09-28
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多