热线电话:13121318867

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

我正在尝试分析wine-quality数据集。有两个数据集: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()

109.0909
4
关注作者
收藏
评论(1)

发表评论
啊啊啊啊啊吖
2019-01-18
解决了,颜色是索引的级别,因此使用它来指定颜色。将我的代码行更改为: counts.plot(kind='bar', title='Counts by Wine Color and quality', color=counts.index.get_level_values(1), alpha=.7)
0.0000 0 0 回复
推荐帖子
条评论