简体   繁体   English

不同颜色的列,Padas DataFrame

[英]Different colors in columns, Padas DataFrame

I have Pandas DataFrame like this:我有这样的 Pandas DataFrame:

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().您可以在 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根据data.fuel的唯一值的大小创建随机颜色列表,并将其传递给plot选项color

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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