简体   繁体   中英

Different colors in columns, Padas DataFrame

I have Pandas DataFrame like this:

data = pd.DataFrame({"fuel":["gas","gas","diesel","diesel","gas","diesel"]})

And I used code to make a bar plot:

ax1 = data.fuel.value_counts().plot('bar')
ax1.set(xlabel = 'Fuel Type', ylabel='Frequency of fuel type')

Nevertheless, I have both column in one colo (blue). What should I do to have different colors of columns?

You can set color parameters in plot().

import pandas as pd
from matplotlib import pyplot as plt

data = pd.DataFrame({"fuel":["gas","gas","diesel","diesel","gas","diesel"]})

data.fuel.value_counts().plot('bar', color=['black', 'red'])
ax1.set(xlabel = 'Fuel Type', ylabel='Frequency of fuel type')

plt.show()

Create a list of random colors by the size of unique values of data.fuel and pass it to option color of plot

colors = [np.random.uniform(size=3) for _ in  range(data.fuel.nunique())]
ax1 = data.fuel.value_counts().plot('bar', color=colors)
ax1.set(xlabel = 'Fuel Type', ylabel='Frequency of fuel type')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM