热线电话:13121318867

登录
2019-01-18 阅读量: 801
直方图的颜色及其标签不一样

问题描述:

有两个数据集:red wine数据集和white wine。我把它们组合在一起形成了wine_df。我想绘制它。我想给红色直方图绘制为红色,白色直方图绘制为白色。但对于某些直方图,其标签和颜色不一致。例如,第四个标签是(4,白色),而其颜色是红色。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

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

colors = [
'red','white']
plt.style.use(
'ggplot')
counts = wine_df.groupby([
'quality', 'color']).count()['pH']
counts.plot(kind=
'bar', title='Counts by Wine Color and quality', color=colors, alpha=.7)
plt.xlabel(
'Quality and Color', fontsize=18)
plt.ylabel(
'Count', fontsize=18)
plt.show()

问题解决:

颜色是索引的级别,因此使用它来指定颜色。将代码行更改为:

counts.plot(kind='bar', title='Counts by Wine Color and quality', 
color=counts.index.get_level_values(
1), alpha=.7)

matplotlib可以将索引中的值解释为颜色。通常,您可以将唯一值映射到可识别的颜色,例如:

color = counts.index.get_level_values(1).map({'red': 'green', 'white': 'black'})
109.0909
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子