How can I rename column in Panda Subplots

116 Views Asked by At

I want to rename the subplot column but I can't do it. I use the code below:

#Encoder variable before plot bar graphs
enc = LabelEncoder()
cols = ['gender', 'children', 'smoker', 'region', 'charges']
X1 = Insurance2[cols].apply(enc.fit_transform)
print(X1)

#bar graphs
attributes = ['gender', 'children', 'smoker', 'region']
  
plt.subplots(figsize=(15, 10))
for i, col in enumerate(attributes):
    plt.subplot(2, 2, i + 1)
    X1.groupby(col).mean()['charges'].plot.bar()
plt.show() 

The result is shown here: I want to change gender from (0,1) to (male, female), smoker from (0,1) to (yes, no) and also region.

Please advise me. Thank you in advance.

After run Code

1

There are 1 best solutions below

4
Panda Kim On BEST ANSWER

Example Code

we need minimal and reproducible example to answer your question. lets make example

import pandas as pd
data1 = {'gender': [0, 0, 1, 1],
         'smoker': [0, 1, 0, 1],
         'charges': [100, 200, 300, 400]}
X1 = pd.DataFrame(data1)

X1

    gender  smoker  charges
0   0       0       100
1   0       1       200
2   1       0       300
3   1       1       400

Only two columns, gender and smoker, were created because I thought that would be enough.


Code

import matplotlib.pyplot as plt

attributes = ['gender', 'smoker'] # only 2 column

# make mapper by column name
m = {'gender': {0: 'male', 1: 'female'}, 'smoker': {0: 'yes', 1: 'no'}}

plt.subplots(figsize=(8, 3))
for i, col in enumerate(attributes):
    plt.subplot(1, 2, i + 1)

    # The result of averaging each column by groupby is set to tmp without drawing a graph.
    tmp = X1.groupby(col)['charges'].mean()

    # A graph is drawn after mapping the index in each group using the variable m.
    tmp.set_axis(tmp.index.map(m[col])).plot.bar()
plt.show()

plot:

enter image description here