MinatoDBO
MinatoDBO

Reputation: 53

How to color bars based on a separate pandas column

I need to plot a barchat and to apply a color according to the "Attribute" column of my dataframe

enter image description here

x axis = Shares
y axis = Price

fig, ax = plt.subplots()
ax.barh(df['Share'],df['Price'], align='center')
ax.set_xlabel('Shares')
ax.set_ylabel('Price')
ax.set_title('Bar Chart & Colors')
plt.show()

Thanks for your help !

Upvotes: 0

Views: 4388

Answers (1)

Trenton McKinney
Trenton McKinney

Reputation: 62373

  • There are two easy ways to plot the bars with separate colors for 'Attribute'
    1. Transform the dataframe with .pivot and then plot with pandas.DataFrame.plot and specify kind='barh' for a horizontal bar plot
      • The index will be the x-axis if using kind='bar', and will be the y-axis if using kind='barh'
      • The columns of the transformed dataframe will each be plotted with a separate color.
      • pandas uses matplotlib as the default plotting backend.
    2. Use seaborn.barplot with hue='Attribute' and orient='h'. This option works with the dataframe in a long format, as shown in the OP.
      • seaborn is a high-level API for matplotlib
  • Tested with pandas 1.3.0, seaborn 0.11.1, and matplotlib 3.4.2

Imports and DataFrame

import pandas as pd
import seaborn as sns

# test dataframe
data = {'Price': [110, 105, 119, 102, 111, 117, 110, 110], 'Share': [110, -50, 22, 79, 29, -2, 130, 140], 'Attribute': ['A', 'B', 'C', 'D', 'A', 'B', 'B', 'C']}
df = pd.DataFrame(data)

   Price  Share Attribute
0    110    110         A
1    105    -50         B
2    119     22         C
3    102     79         D
4    111     29         A
5    117     -2         B
6    110    130         B
7    110    140         C

pandas.DataFrame.plot

# transform the dataframe with .pivot
dfp = df.pivot(index='Price', columns='Attribute', values='Share')

Attribute      A      B      C     D
Price                               
102          NaN    NaN    NaN  79.0
105          NaN  -50.0    NaN   NaN
110        110.0  130.0  140.0   NaN
111         29.0    NaN    NaN   NaN
117          NaN   -2.0    NaN   NaN
119          NaN    NaN   22.0   NaN

# plot
ax = dfp.plot(kind='barh', title='Bar Chart of Colors', figsize=(6, 4))
ax.set(xlabel='Shares')
ax.legend(title='Attribute', bbox_to_anchor=(1, 1), loc='upper left')
ax.grid(axis='x')

enter image description here

  • with stacked=True
ax = dfp.plot(kind='barh', stacked=True, title='Bar Chart of Colors', figsize=(6, 4))

enter image description here

seaborn.barplot

  • Note the order of the y-axis values are reversed compared to the previous plot
ax = sns.barplot(data=df, x='Share', y='Price', hue='Attribute', orient='h')
ax.set(xlabel='Shares', title='Bar Chart of Colors')
ax.legend(title='Attribute', bbox_to_anchor=(1, 1), loc='upper left')
ax.grid(axis='x')

enter image description here

Upvotes: 2

Related Questions