简体   繁体   中英

Create Plots in Matplotlib

New to python and trying to create some plots in matplotlib. My sample data is as below:

df = pd.DataFrame(
    {
        "Date": ["13/07/2020", "13/07/2020", "13/07/2020", "13/07/2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020"],
        "MasterCategory": ["X", "X", "X", "X", "X", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "X", "X", "X", "X", "X", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"],
        "Name": ["sddf", "sdasd", "fdf", "hfh", "rt", "rt", "ter", "ret", "ret", "gh", "fhgf", "r6", "gfh", "etr", "ert", "ery", "yeet", "hfh", "dth", "e", "eryru", "yu", "utyut", "yuy", "uyu", "uyu", "jy", "jyt", "e", "ter", "yeet", "yrt", "y", "yt", "ter", "ytr", "utr", "uru"],
        "SubCategory": ["A", "A", "A", "B", "B", "B", "B", "B", "C", "D", "D", "D", "E", "E", "F", "F", "F", "F", "F", "A", "A", "A", "B", "B", "B", "B", "B", "C", "D", "D", "D", "E", "E", "F", "F", "F", "F", "F"],
        "Age": [24, 35, 23, 34, 5, 56, 65, 44, 64, 24, 35, 23, 34, 5, 56, 65, 44, 64, 45, 21, 19, 34, 34, 3, 40, 48, 37, 70, 20, 35, 23, 34, 5, 56, 65, 44, 64, 45],
        "Exp": [3, 4, 5, 7, 0, 12, 34, 13, 45, 3, 4, 5, 7, 0, 12, 34, 13, 45, 18, 3, 4, 5, 7, 0, 12, 34, 13, 45, 3, 4, 5, 7, 0, 12, 34, 13, 45, 18],
    }
)

My requirement is that I want to create as many figures as there are MaterCategory, and each figure will contain as many rows as there are SubCategory with two columns.The first plot with contain previous week to current week count of Names for each SubCategory and then the second plot will contain the scatter plot for Age and Experience with hue being SubCategory only for the latest week.

The plot should be dynamic as the MasterCategory and SubCategory can be added in due course of time and the Names with the Age and Exp can add up for existing MaterCategory and SubCategory

I have been trying to write the below code to first dynamically find the MasterCategory and then the number of axis to be there in the plot.

dfmastercategory = df['MaterCategory'].unique()
dfsubcategory= df['SubCategory'].unique()
for i in dfmastercategory:
print(f'fig{i}, axis=plt.subplot({len(dfsubcategory)},2,figsize=(5,5))')
for i in dfmastercategory:
    for row in range(len(dfsubcategory)):
        for column in range(2):
            print((f'fig{i} , {row} , {column}'))

The above atleast leads me to that there are 2 MajorCategories and 6 SubCategories. And below is the output which I get.

figX, axis=plt.subplot(6,2,figsize=(5,5))
figX , 0 , 0
figX , 0 , 1
figX , 1 , 0
figX , 1 , 1
figX , 2 , 0
figX , 2 , 1
figX , 3 , 0
figX , 3 , 1
figX , 4 , 0
figX , 4 , 1
figX , 5 , 0
figX , 5 , 1
figY , 0 , 0
figY , 0 , 1
figY , 1 , 0
figY , 1 , 1
figY , 2 , 0
figY , 2 , 1
figY , 3 , 0
figY , 3 , 1
figY , 4 , 0
figY , 4 , 1
figY , 5 , 0
figY , 5 , 1
figY, axis=plt.subplot(6,2,figsize=(5,5))
figX , 0 , 0
figX , 0 , 1
figX , 1 , 0
figX , 1 , 1
figX , 2 , 0
figX , 2 , 1
figX , 3 , 0
figX , 3 , 1
figX , 4 , 0
figX , 4 , 1
figX , 5 , 0
figX , 5 , 1
figY , 0 , 0
figY , 0 , 1
figY , 1 , 0
figY , 1 , 1
figY , 2 , 0
figY , 2 , 1
figY , 3 , 0
figY , 3 , 1
figY , 4 , 0
figY , 4 , 1
figY , 5 , 0
figY , 5 , 1

I then did the below to generate blank plots and getting the following error:

for i in dfmastercategory:
    print(f'fig{i}, axis=plt.subplot({len(dfsubcategory)},2,figsize=(5,5))')
    for i in dfmastercategory:
        for row in range(len(dfsubcategory)):
            for column in range(2):
                ax[row,column].plot()    


index 2 is out of bounds for axis 0 with size 2

Thanks

Having no idea where and how you define ax , I can't give more details why you got the error. I come up with the solution below with no warning and no error.

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame(
    {
        "Date": ["13/07/2020", "13/07/2020", "13/07/2020", "13/07/2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "13-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020", "20-07-2020"],
        "MasterCategory": ["X", "X", "X", "X", "X", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "X", "X", "X", "X", "X", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"],
        "Name": ["sddf", "sdasd", "fdf", "hfh", "rt", "rt", "ter", "ret", "ret", "gh", "fhgf", "r6", "gfh", "etr", "ert", "ery", "yeet", "hfh", "dth", "e", "eryru", "yu", "utyut", "yuy", "uyu", "uyu", "jy", "jyt", "e", "ter", "yeet", "yrt", "y", "yt", "ter", "ytr", "utr", "uru"],
        "SubCategory": ["A", "A", "A", "B", "B", "B", "B", "B", "C", "D", "D", "D", "E", "E", "F", "F", "F", "F", "F", "A", "A", "A", "B", "B", "B", "B", "B", "C", "D", "D", "D", "E", "E", "F", "F", "F", "F", "F"],
        "Age": [24, 35, 23, 34, 5, 56, 65, 44, 64, 24, 35, 23, 34, 5, 56, 65, 44, 64, 45, 21, 19, 34, 34, 3, 40, 48, 37, 70, 20, 35, 23, 34, 5, 56, 65, 44, 64, 45],
        "Exp": [3, 4, 5, 7, 0, 12, 34, 13, 45, 3, 4, 5, 7, 0, 12, 34, 13, 45, 18, 3, 4, 5, 7, 0, 12, 34, 13, 45, 3, 4, 5, 7, 0, 12, 34, 13, 45, 18],
    }
)

dfmastercategory = df['MasterCategory'].unique()
dfsubcategory = df['SubCategory'].unique()

for i in dfmastercategory:
    print(f'fig{i}, axis=plt.subplot({len(dfsubcategory)},2,figsize=(5,5))')

for i in dfmastercategory:
    fig, axis = plt.subplots(nrows=len(dfsubcategory), ncols=2, figsize=(5,5))
    for row in range(len(dfsubcategory)):
        for column in range(2):
            print((f'fig{i} , {row} , {column}'))
            axis[row, column].set_title(dfsubcategory[row])
            axis[row, column].plot()
    fig.tight_layout()
    fig.show()

NOTE : You have some typos in your code

  • MaterCategory in df['MaterCategory'].unique() should be MasterCategory
  • plt.subplot() in fig{i}, axis=plt.subplot({len(dfsubcategory)},2,figsize=(5,5)) should be plt.subplots()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM