Matplotlib / Pandas:如何在同一个图中的不同位置绘制多个散点图?

时间:2016-06-28 12:30:56

标签: python pandas matplotlib plot scatter

我经常有两个pandas数据帧,我想在同一个图中绘制。通常这些是两个样本,我想对比它们的属性,例如:

enter image description here

x轴只有两个位置,左边是第一个数据集,右边是第二个数据集。

在matplotlib中,可以在同一个图中绘制多个数据集:

public class AESWrapper {

    public byte[] encrypt(byte[] plainData, byte[] key) throws AESEncryptionFailedException {

        PaddedBufferedBlockCipher paddedBufferedBlockCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        ParametersWithRandom params = new ParametersWithRandom(new KeyParameter(key));
        paddedBufferedBlockCipher.init(true, params);
        byte[] cipherText = new byte[paddedBufferedBlockCipher.getOutputSize(plainData.length)];

        int val = paddedBufferedBlockCipher.processBytes(plainData, 0, plainData.length, cipherText, 0);

        try {
            paddedBufferedBlockCipher.doFinal(cipherText, val);
        } catch (DataLengthException e) {
            throw new AESEncryptionFailedException("AES Encryption failed due to data length mismatch");
        } catch (IllegalStateException e) {
            throw new AESEncryptionFailedException("AES Encryption failed");
        } catch (InvalidCipherTextException e) {
            throw new AESEncryptionFailedException("AES Encryption failed due to invalid cipher text");
        }

        return cipherText;

    }

    public byte[] decrypt(byte[] cipherText, byte[] key) throws AESDecryptionFailedException {

        PaddedBufferedBlockCipher paddedBufferedBlockCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        ParametersWithRandom params = new ParametersWithRandom(new KeyParameter(key));
        paddedBufferedBlockCipher.init(false, params);
        byte[] plainText = new byte[paddedBufferedBlockCipher.getOutputSize(cipherText.length)];

        int val = paddedBufferedBlockCipher.processBytes(cipherText, 0, cipherText.length, plainText, 0);
        try {
            int len = paddedBufferedBlockCipher.doFinal(plainText, val);
            byte[] decryptedData = new byte[val + len];
            System.arraycopy(plainText, 0, decryptedData, 0, len + val);
            return decryptedData;

        } catch (DataLengthException e) {
            throw new AESDecryptionFailedException("AES Decryption failed due to data length mismatch");
        } catch (IllegalStateException e) {
            throw new AESDecryptionFailedException("AES Decryption failed");
        } catch (InvalidCipherTextException e) {
            throw new AESDecryptionFailedException("AES Decryption failed due to invalid cipher text");
        }
    }
}

enter image description here

然而,

(1)如何将数据集分成两个分区位置,如第一个示例?

(2)你如何用两个pandas数据帧完成这个?你合并它们然后指定两个绘图位置?

1 个答案:

答案 0 :(得分:1)

使用return_type='axes'data1.boxplot返回matplotlib Axes对象。然后使用ax=ax将这些轴传递给第二次调用boxplot。这将导致两个箱图在同一轴上绘制。

ax = df1.plot()
df2.plot(ax=ax) 


a1=a[['a','time']]
ax = a1.boxplot(by='time', meanline=True, showmeans=True, showcaps=True, 
            showbox=True, showfliers=False, return_type='axes')
a2 = a[['c','time']]
a2.boxplot(by='time', meanline=True, showmeans=True, showcaps=True, 
       showbox=True, showfliers=False, ax=ax)