将条形图和折线图与大熊猫结合起来

时间:2017-09-30 13:15:15

标签: python pandas matplotlib dataframe

我试图将条形图和折线图结合起来,但我似乎无法弄明白。我尝试了代码here,但不要透露。我有以下代码和Dataframe:

Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.ComponentModel

Namespace com_test
    Public Partial Class Form1
        Inherits Form

        'displays device
        Private Function PrintDevice(dev As IMMDevice) As String
            Dim propertyStore As IPropertyStore = Nothing
            Dim pDeviceTopology As IDeviceTopology = Nothing
            Dim pConnFrom As IConnector = Nothing
            Dim pConnTo As IConnector = Nothing
            Dim pPart As IPart = Nothing
            Dim pJackDesc As IKsJackDescription = Nothing
            Dim desc As New KSJACK_DESCRIPTION()

            Dim res As New StringBuilder(300)
            Dim o As Object = Nothing

            Dim state As Integer = 0
            Dim con_count As UInteger = 0

            Try
                'device name
                'STGM_READ
                dev.OpenPropertyStore(0, o)
                propertyStore = TryCast(o, IPropertyStore)

                Dim friendlyName As New PropVariant()
                propertyStore.GetValue(Native.PKEY_Device_FriendlyName, friendlyName)
                res.AppendLine(friendlyName.Value.ToString())


                'form factor 
                Dim FormFactor As New PropVariant()
                propertyStore.GetValue(Native.PKEY_AudioEndpoint_FormFactor, FormFactor)
                Dim f As EndpointFormFactor = EndpointFormFactor.UnknownFormFactor
                [Enum].TryParse(Of EndpointFormFactor)(FormFactor.Value.ToString(), f)
                res.AppendLine("Form factor: " + f.ToString())

                dev.GetState(state)

                Dim str As String = ""
                Select Case state
                    Case Native.DEVICE_STATE_DISABLE
                        str = ("Disabled")
                        Exit Select
                    Case Native.DEVICE_STATE_NOTPRESENT
                        str = ("Not present")
                        Exit Select
                    Case Native.DEVICE_STATE_UNPLUGGED
                        str = ("Unplugged")
                        Exit Select
                End Select
                If str <> "" Then
                    res.AppendLine(str)
                End If


                ' DEVICE TOPOLOGY 

                Dim iidDeviceTopology As New Guid("2A07407E-6497-4A18-9787-32F79BD0D98F")
                dev.Activate(iidDeviceTopology, CUInt(CLSCTX.CLSCTX_ALL), IntPtr.Zero, o)
                pDeviceTopology = TryCast(o, IDeviceTopology)

                pDeviceTopology.GetConnector(0, pConnFrom)

                Try
                    o = Nothing
                    pConnFrom.GetConnectedTo(o)
                    pConnTo = TryCast(o, IConnector)

                    pPart = CType(pConnTo, IPart)
                    'QueryInterface
                    Dim iidKsJackDescription As New Guid("4509F757-2D46-4637-8E62-CE7DB944F57B")
                    pPart.Activate(CUInt(CLSCTX.CLSCTX_INPROC_SERVER), iidKsJackDescription, o)
                    pJackDesc = CType(o, IKsJackDescription)

                    If pJackDesc IsNot Nothing Then
                        con_count = 0
                        pJackDesc.GetJackCount(con_count)
                        If con_count > 0 Then
                            Dim sb As StringBuilder

                            'display jacks
                            For i As UInteger = 0 To con_count - 1
                                pJackDesc.GetJackDescription(i, desc)

                                sb = New StringBuilder(100)
                                Dim con_type As EPcxConnectionType = CType(desc.ConnectionType, EPcxConnectionType)
                                Dim loc As EPcxGeoLocation = CType(desc.GeoLocation, EPcxGeoLocation)
                                res.Append("* ")

                                Select Case con_type
                                    Case EPcxConnectionType.eConnType3Point5mm
                                        sb.Append("Jack 3.5 mm ")
                                        Exit Select
                                    Case EPcxConnectionType.eConnTypeAtapiInternal
                                        sb.Append("ATAPI jack")
                                        Exit Select
                                    Case EPcxConnectionType.eConnTypeRCA
                                        sb.Append("RCA jack")
                                        Exit Select
                                    Case EPcxConnectionType.eConnTypeQuarter
                                        sb.Append("1/2 in. jack ")
                                        Exit Select
                                    Case EPcxConnectionType.eConnTypeOtherAnalog
                                        sb.Append("Analog jack ")
                                        Exit Select
                                    Case EPcxConnectionType.eConnTypeOtherDigital
                                        sb.Append("Digital jack ")
                                        Exit Select
                                    Case Else
                                        sb.Append(con_type.ToString() + " ")
                                        Exit Select
                                End Select

                                sb.Append("- " + loc.ToString())
                                'jack location
                                res.Append(sb.ToString())

                                If desc.IsConnected = 0 Then
                                    res.AppendLine(": Disconnected")
                                Else
                                    res.AppendLine(": Connected")


                                End If
                                'end for
                            Next
                        Else
                            res.AppendLine("* No jacks")

                        End If
                    Else
                        res.AppendLine("* Unable to get jacks")
                    End If



                Catch ex As COMException
                    If CUInt(ex.HResult) = &H80070490UI Then
                        'E_NOTFOUND
                        res.AppendLine("Disconnected")
                    Else
                        res.AppendLine("COM error while getting jacks: " + ex.Message)
                    End If
                Catch ex As Exception
                    res.AppendLine("Error while getting jacks: " + ex.Message)

                End Try
            Finally
                'clean up resources                
                If dev IsNot Nothing Then
                    Marshal.ReleaseComObject(dev)
                End If
                If propertyStore IsNot Nothing Then
                    Marshal.ReleaseComObject(propertyStore)
                End If

                If pDeviceTopology IsNot Nothing Then
                    Marshal.ReleaseComObject(pDeviceTopology)
                    pDeviceTopology = Nothing
                End If
                If pConnFrom IsNot Nothing Then
                    Marshal.ReleaseComObject(pConnFrom)
                    pConnFrom = Nothing
                End If
                If pConnTo IsNot Nothing Then
                    Marshal.ReleaseComObject(pConnTo)
                    pConnTo = Nothing
                End If
                If pPart IsNot Nothing Then
                    Marshal.ReleaseComObject(pPart)
                    pPart = Nothing
                End If
                If pJackDesc IsNot Nothing Then
                    Marshal.ReleaseComObject(pJackDesc)
                    pJackDesc = Nothing
                End If
            End Try

            Return res.ToString()
        End Function


        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub button_Click(sender As Object, e As EventArgs)
            Dim devenum As New MMDeviceEnumerator()
            'Create enumerator
            Dim deviceEnumerator As IMMDeviceEnumerator = CType(devenum, IMMDeviceEnumerator)

            Dim defDevice As IMMDevice = Nothing
            Dim propertyStore As IPropertyStore = Nothing

            Try
                Dim o As Object = Nothing

                ' * get default device *                
                deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, o)
                defDevice = TryCast(o, IMMDevice)

                textBox1.Text = "Default sound device: " + Environment.NewLine + Environment.NewLine

                textBox1.Text += PrintDevice(defDevice)
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            Finally
                'clean up resources
                If devenum IsNot Nothing Then
                    Marshal.ReleaseComObject(devenum)
                End If
                If deviceEnumerator IsNot Nothing Then
                    Marshal.ReleaseComObject(deviceEnumerator)
                End If
                If defDevice IsNot Nothing Then
                    Marshal.ReleaseComObject(defDevice)
                End If
                If propertyStore IsNot Nothing Then
                    Marshal.ReleaseComObject(propertyStore)
                End If
            End Try
        End Sub


    End Class
End Namespace

然后我尝试绘制这样的图形:

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

import matplotlib
matplotlib.style.use('ggplot')

%matplotlib inline

dates = pd.date_range('2017-09-16',periods=11)

df = pd.DataFrame({'Elektra_Cost': pd.Series([1.483393,                                                
                                           1.483393,
                                           1.483393,
                                           1.481280,
                                           1.470714,
                                           1.470714,
                                           1.470714,
                                           1.506829,
                                           1.677233,
                                           1.721489,
                                           1.766318], index=dates, dtype='float64'), 
                'Gas_Cost': pd.Series([0.82122857, 
                                        0.82122857, 
                                        0.82122857, 
                                        0.85281429, 
                                        1.01074286, 
                                        1.01074286, 
                                        1.01074286,  
                                        0.92651429,  
                                        1.04047059,  
                                        1.50217941, 
                                        0.58479348],index=dates,dtype='float64'),
                'TG10': pd.Series([10.3, 
                                   11.0,
                                   11.3,
                                   12.0,  
                                   13.0,
                                   13.1,
                                   12.8,
                                   11.1,  
                                   13.5,
                                   14.1,  
                                   13.3],index=dates,dtype='float64'), 
                'TN10': pd.Series([5.8, 
                                   4.3, 
                                   9.0,
                                   7.5, 
                                   8.2,
                                   7.9, 
                                   6.0, 
                                   4.3, 
                                   4.6, 
                                   8.5, 
                                   8.8],index=dates,dtype='float64'), 
                'TX10': pd.Series([15.7,  
                                   17.3,  
                                   15.4,  
                                   17.3,  
                                   18.5,  
                                   19.2,  
                                   20.0,  
                                   18.2,  
                                   20.6,
                                   18.9,  
                                   18.2],index=dates,dtype='float64'),

               })

结果如下图所示:

plot

为什么我没有在图表中看到线条(TG10,TN10和TX10)?

我在jupyter笔记本中运行它

更新

第一个建议link就行了。所以我最终这样做了:

ax = df[['TG10', 'TN10', 'TX10']].plot(figsize=(20,15), linestyle='--', secondary_y=['TG10', 'TN10', 'TX10'])
df[['Elektra_Cost', 'Gas_Cost']].plot(figsize=(20,15), kind='bar', ax=ax)
plt.show()

导致:

plot

0 个答案:

没有答案