Reputation: 1539
I have a program that is analyzing a dataset from a pandas dataframe.
The following is part of the program and it works perfectly:
selected_columns = ['cap-color', 'gill-color', 'veil-color', 'spore-print-color']
for col in selected_columns:
x = X[col].value_counts()
x_vals = x.values.tolist()
cap_color_list = ['brown', 'gray', 'red', 'yellow', 'white', 'buff', 'pink', 'cinnamon', 'green', 'purple']
gill_color_list = ['buff', 'pink', 'white', 'brown', 'gray', 'chocolate', 'purple', 'black', 'red', 'yellow', 'orange', 'green']
veil_color_list = ['white', 'brown', 'orange', 'yellow']
spore_print_color_list = ['white', 'brown', 'black', 'chocolate', 'green', 'purple', 'orange', 'buff', 'yellow']
plt.figure(figsize=(8, 6), facecolor='0.9')
x.plot(kind='bar', width=0.7, color='lightgreen', edgecolor='darkgreen')
if col == 'cap-color':
plt.xticks([i for i in range(len(x))], cap_color_list, rotation=0)
plt.title('cap-color', fontsize=15, color='darkred', weight='bold')
if col == 'gill-color':
plt.xticks([i for i in range(len(x))], gill_color_list, rotation=0)
plt.title('gill-color', fontsize=15, color='darkred', weight='bold')
if col == 'veil-color':
plt.xticks([i for i in range(len(x))], veil_color_list, rotation=0)
plt.title('veil-color', fontsize=15, color='darkred', weight='bold')
if col == 'spore-print-color':
plt.xticks([i for i in range(len(x))], spore_print_color_list, rotation=0)
plt.title('spore-print-color', fontsize=15, color='darkred', weight='bold')
for i, number in enumerate(x_vals):
plt.text(x=i, y=number, s=str(number), horizontalalignment='center', fontsize=10, weight='bold')
plt.xlabel("Color", size=16, labelpad=8, color='darkred')
plt.ylabel("Count", size=16, labelpad=8, color='darkred')
plt.show()
Sample output (just one, not all four):
Note how the light grey face color is applied.
Later in the program I have this block of code:
# plt.clf()
plt.figure(figsize=(8, 6), facecolor='0.9')
TEMP.plot(kind='bar', width=0.9)
for i, number in enumerate(TEMP['e']):
plt.text(x=i - 0.25, y=number + 0.9, horizontalalignment='center', s=round(number, 2), fontsize=8, weight='bold')
for i, number in enumerate(TEMP['p']):
plt.text(x=i + 0.22, y=number + 0.9, horizontalalignment='center', s=round(number, 2), fontsize=8, weight='bold')
plt.xticks([i for i in range(6)], population, rotation=0)
plt.title('Population', fontsize=15, color='darkred', weight='bold')
# plt.ylim(top=3000)
plt.xlabel("Population Type", size=16, labelpad=8, color='darkred')
plt.ylabel("Count", size=16, labelpad=8, color='darkred')
plt.show()
I get this result:
This entire line does nothing:
plt.figure(figsize=(8, 6), facecolor='0.9')
No matter what I change the figsize values to, nothing changes. The facecolor='0.9' is also not being applied.
Everything else about the second bar chart is correct.
Why am I unable to control the figsize and facecolor in the second bar chart?
The second bar chart is being created from this temp df:
print('*' * 26)
TEMP = df.groupby('class')['population'].value_counts().unstack(level=1).fillna(0).T.sort_values('e', ascending=False)
TEMP = TEMP.astype(int)
print(TEMP)
print('*' * 26, '\n')
Output:
**************************
class e p
population
v 1192 2848
y 1064 648
s 880 368
n 400 0
a 384 0
c 288 52
**************************
Upvotes: 2
Views: 6606
Reputation: 62403
pandas.DataFrame.plot
returns an Axes
, and has a figsize
parameter.facecolor
is used as a parameter inside the DataFrame.plot
ax = x.plot(kind='bar', width=0.7, color='lightgreen', edgecolor='darkgreen', figsize=(8, 6))
ax = TEMP.plot(kind='bar', width=0.9, figsize=(8, 6))
facecolor
is required, use this implementation.fig, axe = plt.subplots(figsize=(8, 6), facecolor='0.9')
x.plot(kind='bar', width=0.7, color='lightgreen', edgecolor='darkgreen', ax=axe)
fig, axe = plt.subplots(figsize=(8, 6), facecolor='0.9')
TEMP.plot(kind='bar', width=0.9, ax=axe)
Upvotes: 2