在多个子图中一起广播的两个数据帧

时间:2018-08-30 17:49:39

标签: python pandas matplotlib plot

我想绘制5个不同的子图,每年的数据一个。对于每一年,我想显示每个县的DEM和REP。到目前为止,我已经编写了以下代码:

    [DllImport(@"C:\Program Files (x86)\DLL\UFDF.dll", EntryPoint = "UF01_OpenDevices", CallingConvention = System.Runtime.InteropServices.CallingConvention.Winapi)]
    unsafe static extern Boolean UF01_OpenDevices(byte* devices);

    [DllImport(@"C:\Program Files (x86)\DLL\UFDF.dll", EntryPoint = "UF01_CloseAll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Winapi)]
    unsafe static extern void UF01_CloseAll();

    [DllImport(@"C:\Program Files (x86)\SELIAtec\DLL\UFDF.dll", EntryPoint = "UF01_UD01_8Entrees", CallingConvention = System.Runtime.InteropServices.CallingConvention.Winapi)]
    unsafe static extern Boolean UF01_UD01_8Entrees(byte Device, byte module, byte* data);

    [DllImport(@"C:\Program Files (x86)\DLL\UFDF.dll", EntryPoint = "UF01_UD01_8Entrees", CallingConvention = System.Runtime.InteropServices.CallingConvention.Winapi)]
    unsafe static extern Boolean UF01_GetInfosModule(byte Device, bool Watchdog, [In, Out] _UF01_INFORMATIONS infos);

    public Form1()
    {
        InitializeComponent();

    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    public struct _UF01_INFORMATIONS
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public unsafe byte[] Type;
        [MarshalAs(UnmanagedType.I1)]
        public unsafe byte Nombre_Module;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public unsafe byte[] Nb_Voies;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public unsafe byte[] Modele;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public unsafe string[] Name;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public unsafe string[] Comments;
        [MarshalAs(UnmanagedType.I1)]
        public unsafe bool UseInterrupt;
    }


    private unsafe void button1_Click(object sender, EventArgs e)
    {
        byte nb_Device;

        _UF01_INFORMATIONS m_infos = new _UF01_INFORMATIONS();
        m_infos.Type = new byte[8];
        m_infos.Nb_Voies = new byte[8];
        m_infos.Modele = new byte[8];
        m_infos.Name = new string[8];
        m_infos.Comments = new string[8];
        m_infos.Nombre_Module = 0;
        m_infos.UseInterrupt = false;


        if (UF01_OpenDevices(&nb_Device))
        {
            this.label1.Text = "Nombre de CPU's : " + nb_Device;

            UF01_GetInfosModule(nb_Device, false, m_infos); //<== Here is the ERROR

            label2.Text = "Nombre de modules : " + m_infos.Nombre_Module;
         }
    }

但是,就像第一个子图(3,2,1)一样,我得到了错误: fig = plt.figure(figsize=(13,10)) plt.subplot(3, 2, 1) plt.bar(data=districts_2018[['DEM', 'REP']], x=districts_2018.index, height=(districts_2018[['DEM', 'REP']])) plt.subplot(3, 2, 2) plt.bar(data=districts_2017[['DEM', 'REP']], x=districts_2017.index, height=districts_2017['DEM']) plt.subplot(3, 2, 3) plt.bar(data=districts_2016[['DEM', 'REP']], x=districts_2016.index, height=districts_2016['DEM']) plt.subplot(3, 2, 4) plt.bar(data=districts_2015[['DEM', 'REP']], x=districts_2015.index, height=districts_2015['DEM']) plt.subplot(3, 2, 5) plt.bar(data=districts_2014[['DEM', 'REP']], x=districts_2014.index, height=districts_2014['DEM']) plt.tight_layout(); 。如果我仅将高度设置为ValueError: shape mismatch: objects cannot be broadcast to a single shape,则此代码有效,然后仅显示DEM,而不显示REP。

1 个答案:

答案 0 :(得分:1)

您可以直接使用熊猫包装纸来绘制分组的条形图。将ax设置为要在其中显示该图的各个子图。

fig, axes = plt.subplots(3,2,figsize=(13,10))
for ax, df in zip(axes.flat, [districts_2018, districts_2017, ....])
    df[['DEM', 'REP']].plot.bar(ax=ax)