Change language

Various image using pandas and matplotlib

Use these commands to install matplotlib, pandas, and numpy:

 pip install matplotlib pip install pandas pip install numpy 

Parcel types:

  1. Basic plotting. In this basic plotting, we can use randomly generated data to plot using series and matplotlib.

    # import libraries

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

     

    ts = pd.Series (np.random.randn ( 1000 ), in dex = pd.date_range (

    ’1/1 / 2000’ , periods = 1000 ))

    ts = ts.cumsum ()

    ts.plot ()

     
    plt.show ()

    Output:

  2. Different data plot: using more than one list of data in a plot.

    # importing libraries

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

      

    ts = pd.Series ( np.random.randn ( 1000 ), index = pd.date_range (

    ’1/1 / 2000’ , periods = 1000 ))

     

    df = pd.DataFrame (np.random.randn ( 1000 , 4 ), 

    index = ts.index, columns = list ( ’ABCD’ ))

     

    df = df.cumsum ()

    plt.figure ()
    df .plot ()
    plt.show ()

    Output:

    , Li"

  3. Plot on a given axis: we can explicitly define an axis name and plot data based on that axis.

    # importing libraries

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

     

    ts = pd.Series (np .random.randn ( 1000 ), index = pd.date_range (

    ’1/1/2000 ’ , periods = 1000 ))

      

    df = pd.DataFrame (np .random.randn ( 1000 , 4 ), index = ts.index,

    columns = list ( ’ABCD’ ))

     

    df3 = pd.DataFrame (np.random.randn ( 1000 , 2 ),

      columns = [ ’B’ , ’ C’ ]). cumsu m ()

     

    df3 [ ’A’ ] = pd.Series ( list ( range ( len (df))))

    df3.plot (x = ’ A’ , y = ’B’ )

    plt.show ()

    Output:

  4. Histogram using matplotlib: Look for different types of histogram to clearly understand the behavior of the data.

    # importing libraries

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

     

    ts = pd.Series (np.random.randn ( 1000 ), index = pd.date_range (

    ’1/1 / 2000’ , periods = 1000 ))

     

    df = pd.DataFrame (np.random.randn ( 1000 , 4 ), index = ts.index,

    columns = list ( ’ ABCD’ ))

     

    df3 = pd.DataFrame (np.random.randn ( 1000 , 2 ),

    columns = [ ’B’ , ’ C’ ]). cumsum ()

     

    df3 [ ’A’ ] = pd.Series ( list ( range ( len (df))))

    df3.iloc [ 5 ]. plot.bar ()

    plt.axhline ( 0 , color = ’k ’ )

      
    plt.show ()

    Output:

  5. Histograms:

    # import libraries

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

      

    df4 = pd.DataFrame ({ ’a’ : np.random.randn ( 1000 ) + 1

    ’b’ : np.random.randn ( 1000 ), 

    ’c’ : np.random.randn ( 1000 ) - 1 },

    columns = [ ’ a’ , ’b’ , ’c’ ])

    plt.figure ()

     

    df4.plot.hist (alpha = 0.5 )

    plt.show ()

    Exit:

  6. Plot using Series and matplotlib: use a block to plot the data.

    # importing libraries

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

      

    df = pd .DataFrame (np.random.rand ( 10 , 5 ), 

    columns = [ ’A’ , ’B’ , ’ C’ , ’ D’ , ’E’ ])

     
    df.plot .box ()
    plt.show ()

    Output:

  7. Plot density:

    # importing libraries

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

     

    df = pd.DataFrame (np.random.rand ( 10 , 5 ), 

    columns = [ ’A’ , ’ B’ , ’C’ , ’D’ , ’ E’ ])

     

    ser = pd.Series (np.random.randn ( 1000 ))

    ser.plot.kde ()

     
    plt.show ()

    Output:

  8. Plot area using matplotlib:

    # library import

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

     

    df = pd.DataFrame (np.random.ran d ( 10 , 5 ), 

      columns = [ ’A’ , ’B’ , ’ C’ , ’D’ , ’ E’ ])

      
    df.plot.area ()
    plt.show ()

    Output:

  9. Dot plot:

    # importing libraries

    import matplotlib .pyplot as plt

    import pandas as pd

    import numpy as np

     

    df = pd.DataFrame (np.random.rand ( 500 , 4 ),

    columns = [ ’a’ , ’b’ , ’ c’ , ’d’ ])

     

    df.plot.scatter (x = ’a’ , y = ’ b’ )

    plt.show ()

    Output:

  10. Hexagonal bunker

    # importing libraries

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

     

    df = pd.DataFrame (np.random.randn ( 1000 , 2 ) , columns = [ ’a’ , ’b’ ])

     

    df [ ’a’ ] = df [ ’ a’ ] + np.arange ( 1000 )

    df.plot.hexbin (x = ’ a’ , y = ’b’ , gridsize = 25 )

    plt.show ()

    Output:

  11. Circular plot:

    # import libraries

    import matplotlib.pyplot as plt

    import pandas as pd

    import numpy as np

     

    series = pd.Series ( 3 * np.random.rand ( 4 ) ,

    index = [ ’a’ , ’b’ , ’ c’ , ’d’ ], name = ’ series’ )

     

    series.plot.pie (figsize = ( 4 , 4 ))

    plt.show ()

    Output:

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically