Having problem to set the color of the two histograms separately

Viewed 127

I'm trying to analyze the wine-quality dataset. There are two datasets: the red wine dataset and the white wine. I combine them together to form the wine_df. And I want to see the average quality of the wine group by color, I try to plot them. And I want to give the red histogram red color, the white histogram white color. But these two histograms are always the same color. It seems that the 'color' parameter in the plot() doesn't work. And the result of the color is always the first one I set in the list colors. What should I do? Thanks for your answer!

python
colors = ['white','red']
plt.style.use('ggplot')
color_means = wine_df.groupby('color')['quality'].mean()
color_means.plot(kind='bar', title='Average Wine Quality by Color', color=colors, alpha=.7)
plt.xlabel('colors', fontsize=18)
plt.ylabel('Quality', fontsize=18)

enter image description here

1 Answers

I am not sure how your data looks like. However, I used the data from the github and added a new column to each dataframe with the value associated with its color. That is, red value for red_wine and white value for white_wine.

Code:

## Data read
red_wine = pd.read_csv('https://raw.githubusercontent.com/nishanthgandhidoss/Wine-Quality/master/data/winequality-red.csv',
                      sep = ';')
white_wine = pd.read_csv('https://raw.githubusercontent.com/nishanthgandhidoss/Wine-Quality/master/data/winequality-white.csv', 
                        sep = ';')

## Add a column to each data to identify the wine color 
red_wine['color'] = 'red'
white_wine['color'] = 'white'

## Combine the two dataframes    
wine_df = pd.concat([red_wine, white_wine])

## Histogram with different colors
colors = ['red','white']
plt.style.use('ggplot')
color_means = wine_df.groupby('color')['quality'].mean()
color_means.plot(kind='bar', title='Average Wine Quality by Color', color=colors, alpha=.7)
plt.xlabel('colors', fontsize=18)
plt.ylabel('Quality', fontsize=18)

Output:

enter image description here

Related