绘制多索引数据帧条形图颜色由类别决定

2024-10-02 00:42:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个多索引数据帧,看起来像下面的数据。当我绘制数据时,图表如下所示。在

如何绘制条形图,其中条形图的颜色取决于我想要的类别(例如:“城市”)。因此,属于同一个城市的所有酒吧都有相同的颜色,不分年份。例如:在下图中,所有ATL栏都应该是红色的,而MIA栏应该是蓝色的。在

enter image description here

City            ATL                                    MIA               \
Year           2010         2011         2012         2010         2011   
Taste                                                                     
Bitter  3159.861983  3149.806667  2042.348937  3124.586470  3119.541240   
Sour    1078.897032  3204.689424  3065.818991  2084.322056  2108.568495   
Spicy   5280.847114  3134.597728  1015.311288  2036.494136  1001.532560   
Sweet   1056.169267  1015.368646  4217.145165  3134.734027  4144.826118   

City                 
Year           2012  
Taste                
Bitter  1070.925695  
Sour    3178.131540  
Spicy   3164.382635  
Sweet   3173.919338 

以下是我的代码:

^{pr2}$

Tags: 数据city颜色图表绘制类别year条形图
2条回答

编辑包括颜色循环和任意数量的城市

您将需要指定一些额外的参数以使其看起来更漂亮,但类似这样的方法可能会起作用

import itertools # for color cycling

# specify the colors you want for each city
color_cycle = itertools.cycle( plt.rcParams['axes.color_cycle']  )
colors = { cty:color_cycle.next() for cty in DF.City.unique() }

#spcify the relative position of each bar
n = len(list(DFGSum))
positions = linspace(-n/2., n/2., n)

# plot each column individually
for i,col in enumerate(list(DFGSum)):
    c = colors[col[0]]
    pos = positions[i]
    DFGSum[col].plot(kind='bar', color=c, 
                     position=pos, width=0.05)

plt.legend()
plt.show()

enter image description here

虽然在这里你无法分辨哪个酒吧对应哪一年。。。在

替代方案

你也可以做一个稍微不同的类型的图,在勾号标签中保留年份信息。这可以推广到任何数量的城市,并将保持默认的颜色样式

^{pr2}$

enter image description here

我已经找到了解决我自己问题的办法。我对最初回答我问题的@dermen给予了部分信任。我的回答受到了他的启发。在

虽然@dermen的解决方案是正确的,但我觉得我需要一种方法,我不必手动调整栏的宽度或担心位置。在

下面的解决方案可以适用于任意数量的城市,以及属于该城市的年度数据。重要的是要知道,在下面的解决方案中,绘制的数据帧是多级数据帧。在数据帧被排序的情况下,解决方案可能会中断,因为打印是按特定的顺序进行的。在

enter image description here

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import random

matplotlib.style.use('ggplot')


taste = ['Sweet','Spicy','Sour','Bitter']
store = ['Asian','Italian','American','Greek','Mexican']

df1 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})

df2 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})

df3 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})

df4 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})

df5 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})


df6 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})


df7 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})


df8 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})

df9 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})


df10 = pd.DataFrame({'Taste':[random.choice(taste) for x in range(10)],
                   'Store':[random.choice(store) for x in range(10)],
                   'Sold':1000+100*np.random.rand(10)})



df1['Year'] = '2010'
df1['City'] = 'MIA'

df2['Year'] = '2011'
df2['City'] = 'MIA'

df3['Year'] = '2012'
df3['City'] = 'MIA'

df4['Year'] = '2010'
df4['City'] = 'ATL'

df5['Year'] = '2011'
df5['City'] = 'ATL'

df6['Year'] = '2012'
df6['City'] = 'ATL'


df7['Year'] = '2013'
df7['City'] = 'ATL'

df8['Year'] = '2014'
df8['City'] = 'ATL'

df9['Year'] = '2013'
df9['City'] = 'CHI'

df10['Year'] = '2014'
df10['City'] = 'CHI'

DF = pd.concat([df1,df2,df3,df4,df5,df6,df7,df8,df9,df10])

DFG = DF.groupby(['Taste', 'Year', 'City'])
DFGSum = DFG.sum().unstack(['Year','City']).sum(axis=1,level=['City','Year'])
#DFGSum is a multilevel DataFrame 

import itertools 
color_cycle = itertools.cycle( plt.rcParams['axes.color_cycle']  )

plot_colors = [] #Array for a squenece of colors to be plotted 

for city in DFGSum.columns.get_level_values('City').unique(): 
  set_color = color_cycle.next() #Set the color for the city 
  for year in DFGSum[city].columns.get_level_values('Year').unique():
    plot_colors.append(set_color)
    #For each unqiue city, all the yearly data belonging to that city will have the same color 

DFGSum.plot(kind='bar',color=plot_colors)
# The color pramater of the plot function allows a list of colors sequences to be specified

相关问题 更多 >

    热门问题