Why The Title And The X-label For The Charts Are All The Same Even I Already Included Them In The For Loop?

- 1 answer

I want the bar chart to have their own title and x-axis label and so I included the plt.title and plt.xlabel in the for loop.

However, the title and x-axis label are the same for both graph after I run the code. The title for the first graph should be Histogram of gender and the title for the 2nd graph should be Histogram of job. What's wrong with my code or which part I did wrong especially the loop?

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
from scipy import stats

# first data is age
# 2nd data is gender
# third data is saving
# 4th data is job

data = np.array([[11, "male",1222,"teacher"],[23,"female",333,"student"],
                 [22, "male", 221, "teacher"],[27, "male", 333, "agent"],[11, "female", 33, "farmer"]])

data_feature_names = ["age","gender","saving","job"]

# type of the data above
types = ["num","cat","num","cat"]
idx2 = []

for index, _type in enumerate(types):
    if _type == 'cat':

# Order of x axis label
ss = [["female","male"],["farmer","agent","security","teacher","student"]]

for k in range(0,len(ss)):
    for j in idx2:
        pandasdf = pd.DataFrame(data)
        sns.countplot(x=j, data=pandasdf, order = ss[k])
        plt.title("Histogram of " + data_feature_names[j])


Your ordering in ss and column names in idx2 are paired, so you can use a single loop and end up with the desired results for the gender and job histograms (but in this case you won't end up with histograms for age or saving). The last few lines of your example would become:

for k, j in zip(range(0, len(ss)), idx2):
    pandasdf = pd.DataFrame(data)
    sns.countplot(x=j, data=pandasdf, order=ss[k])
    plt.title("Histogram of " + data_feature_names[j])

There's a few ways you could simplify this such that it's a bit easier to debug. For example, you could shorten the loop for idx2 by using a list comprehension:

idx2 = [ix for ix, f in enumerate(types) if f == "cat"]

I've provided an additional example below with a few less lines of code, but more modification from the original script.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

data = pd.DataFrame(
        [11, "male", 1222, "teacher"],
        [23, "female", 333, "student"],
        [15, "male", 542, "security"],
        [23, "male", 4422, "farmer"],
        [25, "female", 553, "farmer"],
        [22, "male", 221, "teacher"],
        [27, "male", 333, "agent"],
        [11, "female", 33, "farmer"],
    columns=["age", "gender", "saving", "job"],

ordering = {
    "gender": ["female", "male"],
    "job": ["farmer", "agent", "security", "teacher", "student"],

for column in ['gender', 'job']:
    ax = sns.countplot(x=column, data=data, order=ordering.get(column, None))
    ax.set_title("Histogram of {}".format(column))