简体   繁体   English

在 barh 中更改 colors(堆叠)

[英]changing colors in barh(stacked)

I have one table with regions (y axes) and in x values.我有一张包含区域(y 轴)和 x 值的表格。 Each region has two bars.每个区域都有两个条形图。 I would like to change colors. I would like that each little bar in barh has a different color.我想更改 colors。我希望 barh 中的每个小条都有不同的颜色。 I have this code where colors repeat: For example after pink I would like another color, not red again.It is possible to change the colors scale?我有这段代码,其中 colors 重复:例如,在粉红色之后我想要另一种颜色,而不是红色。可以更改 colors 比例吗? using for example "tab10" Furthemore, it is possible to get legend where each color outline one year (2010,2011,2012,2013,2014,2015,2016,2017,2018,2019)使用例如“tab10”此外,可以获得每种颜色轮廓一年的图例(2010,2011,2012,2013,2014,2015,2016,2017,2018,2019)

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

matplotlib.style.use('ggplot')

plotdata = pd.DataFrame({
    "2010_y":[0.63,0.56,0.89,0.94,0.68,0.63,0.34,0.54,0.77,0.77,0.86,0.42,0.49,0.70,1.12,1.10,0.30,0.67,0.30,0.23],
    "2011_y":[1.10,0.54,0.94,0.81,0.80,0.87,0.90,0.99,0.53,0.78,0.98,0.99,0.87,1.09,1.18,0.89,0.89,0.70,1.05,0.86],
    "2012_y":[1.39,0.97,0.98,0.99,0.95,1.10,1.07,1.10,1.02,1.05,0.97,0.92,0.94,0.91,0.85,1.22,1.24,1.08,1.08,1.01],
    "2013_m":[1.86,1.34,1.12,1.25,1.26,1.08,1.31,1.58,1.22,1.26,1.37,1.14,1.23,1.00,1.13,1.49,1.14,1.37,1.25,1.23],
    "2014_m":[1.59,1.04,1.03,1.10,1.44,1.43,1.33,1.81,1.56,1.22,1.08,1.36,1.11,0.87,1.10,0.68,1.31,1.26,1.25,1.61],
    "2015_m":[0.71,1.14,1.18,0.99,0.87,0.74,0.91,0.57,0.79,1.09,1.16,0.89,1.15,0.98,1.24,0.75,0.85,0.83,1.02,0.71],
    "2016_m":[0.67,1.22,1.00,0.96,0.97,0.96,0.95,0.79,1.06,1.04,0.83,1.06,1.07,0.98,0.66,1.06,0.99,1.13,1.03,1.05],
    "2017_m":[0.68,0.84,0.73,0.73,0.79,0.98,0.80,0.54,0.85,0.91,0.55,0.65,0.86,0.71,0.71,0.77,0.95,0.80,0.81,0.87],
    "2018_m":[0.74,1.28,1.21,1.13,0.99,1.00,1.21,0.97,1.07,0.91,1.38,1.34,1.25,1.65,1.28,0.97,1.11,1.09,1.20,1.13],
    "2019_m":[0.63,1.07,0.92,1.10,1.24,1.21,1.18,1.10,1.12,0.97,0.80,1.24,1.04,1.11,0.74,1.06,1.23,1.07,1.01,1.30]
    }, index=["ABR", "BAS", "CAL", "CAM", "EMR","FVG","LAZ","LIG","LOM","MAR","MOL","PIE","PUG","SAR","SIC","TOS","TAA","UMB","VDA","VEN"]
)
plotdata3 = pd.DataFrame({
    "2010_y":[4.12,1.44,5.73,3.91,3.43,0.00,4.26,0.00,1.95,2.65,0.00,4.82,3.61,2.17,3.05,2.66,0.00,2.86,3.00,1.15],
    "2011_y":[0.00,0.39,0.00,0.00,0.00,1.32,0.00,0.00,1.02,0.00,2.72,0.00,0.00,0.00,0.83,0.00,0.00,0.00,0.00,0.00],
    "2012_y":[0.08,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
    "2013_m":[0.00,0.00,0.00,0.00,0.45,0.00,0.00,2.58,0.00,0.00,0.00,0.00,0.00,1.90,0.00,0.00,0.00,0.51,0.00,0.00],
    "2014_m":[0.05,0.03,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.40,0.00,0.01,0.00,0.00,0.42,0.00,0.00,0.00,0.00,0.00],
    "2015_m":[1.45,0.16,0.27,0.09,0.64,1.68,0.20,2.42,0.47,0.41,0.28,0.32,0.09,0.93,0.18,0.52,0.00,0.67,0.00,0.00],
    "2016_m":[0.00,0.89,0.00,0.00,1.11,0.00,0.00,0.00,0.00,0.44,0.00,0.03,0.00,0.00,0.00,0.00,0.00,0.22,0.00,0.00],
    "2017_m":[0.00,0.00,0.00,0.00,0.95,0.00,0.00,0.00,0.09,1.85,0.00,0.00,0.11,0.00,0.07,0.35,0.00,1.74,0.00,0.00],
    "2018_m":[0.00,0.00,0.00,0.00,0.06,0.00,0.11,0.00,0.00,0.00,0.00,0.25,0.00,0.00,0.01,0.00,0.00,0.00,0.00,0.00],
    "2019_m":[0.30,3.09,0.00,0.00,0.34,0.00,0.42,0.00,1.48,0.25,0.00,0.58,0.19,0.00,2.44,0.46,0.00,0.00,0.00,0.85]
    }, index=["ABR", "BAS", "CAL", "CAM", "EMR","FVG","LAZ","LIG","LOM","MAR","MOL","PIE","PUG","SAR","SIC","TOS","TAA","UMB","VDA","VEN"]
)
fig, ax = plt.subplots()
#stacked_data = plotdata.apply(lambda x: x*100/sum(x), axis=1)
#stacked_data2 = plotdata2.apply(lambda x: x*100/sum(x), axis=1)
#stacked_data3 = plotdata3.apply(lambda x: x*100/sum(x), axis=1)

stacked_data.plot(kind="barh", stacked=True, width=0.4, 
                  ax=ax, position=0, edgecolor='black')
#stacked_data2.plot(kind="barh", stacked=True, width=0.25, 
                  # ax=ay, position=1, hatch='//',edgecolor='black')

stacked_data3.plot(kind="barh", stacked=True, width=0.4, 
                   ax=ax, position=1,edgecolor='black')
ax.get_legend().remove()
ax.set_ylim(top=len(stacked_data)-0.1)
#ax.set_xlim(right=len(stacked_data)-0.5)
ax.set_facecolor('xkcd:white')
# displaying the title
plt.title("titla")
# set various colors
ax.spines['bottom'].set_color('black')
ax.spines['top'].set_color('black') 
ax.spines['right'].set_color('black')
ax.spines['left'].set_color('black')

在此处输入图像描述

You can specify your colormap when plotting.您可以在绘图时指定颜色图。 Also use different colormaps for your barplots if you like, for example:如果您愿意,还可以为您的条形图使用不同的颜色图,例如:

stacked_data3.plot(kind="barh", stacked=True, width=0.4, 
                   ax=ax, position=1,edgecolor='black', cmap='Accent')

A list of available colormaps is available on matplotlib.org matplotlib.org上提供了可用颜色图列表

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

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