根据颜色图着色的条形图? [英] Barplot colored according a colormap?

查看:95
本文介绍了根据颜色图着色的条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对Matplotlib或Seaborn中的颜色很陌生.我的目的是创建一个根据自定义调色板上色的条形图.像这样,但是有了我的自定义调色板(见下文,带有红色,橙色,绿色和蓝色的调色板):

First of all, I'm pretty new to colors in Matplotlib or Seaborn. My purpose is to create a barplot with bars coloured according to a custom palette. Something like this, but with my custom palette (see below, a palette with red, orange, green and blue):

我已经使用LinearSegmentedColormap方法创建了自定义顺序调色板,但是我无法在简单的plt.barplot()中使用它.当然,这并不困难,但我看不出路.我使用以下来自以下线程的函数创建了调色板:使用matplotlib创建自己的颜色图并绘制颜色比例

I have created my custom sequential palette using the LinearSegmentedColormap method, but I'm not able to use it in a simple plt.barplot(). Sure it's not difficult, but I can't see the way. I created the palette using the function below, got from this thread: Create own colormap using matplotlib and plot color scale

def make_colormap(seq):
"""Return a LinearSegmentedColormap
seq: a sequence of floats and RGB-tuples. The floats should be increasing
and in the interval (0,1).
"""
seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
cdict = {'red': [], 'green': [], 'blue': []}
for i, item in enumerate(seq):
    if isinstance(item, float):
        r1, g1, b1 = seq[i - 1]
        r2, g2, b2 = seq[i + 1]
        cdict['red'].append([item, r1, r2])
        cdict['green'].append([item, g1, g2])
        cdict['blue'].append([item, b1, b2])

return mcolors.LinearSegmentedColormap('CustomMap', cdict)

#main#
c = mcolors.ColorConverter().to_rgb
rvb = make_colormap(
[c('red'), 0.125, c('red'), c('orange'), 0.25, c('orange'),c('green'),0.5, c('green'),0.7, c('green'), c('blue'), 0.75, c('blue')])

N = 1000
array_dg = np.random.uniform(0, 10, size=(N, 2))
colors = np.random.uniform(0, 5, size=(N,))
plt.scatter(array_dg[:, 0], array_dg[:, 1], c=colors, cmap=rvb)
plt.colorbar()
plt.show()

返回此图:

据我了解,我不能将色图(LinearSegmentedColormap()中的对象类型?)用于条形图,但是色图是我获得自定义顺序调色板的独特方式.

As far as I can understand, I can't use a colormap (object type from LinearSegmentedColormap()? ) for barplots, but colormap is the unique way I have achieved a custom sequential palette.

总而言之,我想将第二个图(散点图)的色图应用于第一个图(散点图).现在我不能这样做,因为barplot()函数没有一个接受LinearSegmentedColormap对象类型的参数.

In summary, I want to apply the colormap of the second plot (the scatterplot) to the first plot (the barplot). For now I can't do it because the barplot() function has not an argument that accepts a LinearSegmentedColormap object type.

我可能会比实际做起来更难,因此,我希望使用任何更清洁或更正确的方法.

I'm probably making it harder than it really is, so I would appreciate any cleaner or more correct way.

推荐答案

要获得带有根据颜色图着色的条形图的条形图,可以使用bar(x,y, color=colors)color参数,其中colors是条的长度数,包含所有颜色. IE.该列表中的第i个条目是第i条的颜色.
为了从颜色图创建此列表,您需要使用各自的值调用颜色图.

To obtain a barplot with the bars colored according to a colormap you can use the color argument of bar(x,y, color=colors), where colors is a list of length number of bars, containing all the colors. I.e. the ith entry in that list is the color for the ith bar.
In order to create this list from the colormap, you need to call the colormap with the respective value.

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np

clist = [(0, "red"), (0.125, "red"), (0.25, "orange"), (0.5, "green"), 
         (0.7, "green"), (0.75, "blue"), (1, "blue")]
rvb = mcolors.LinearSegmentedColormap.from_list("", clist)

N = 60
x = np.arange(N).astype(float)
y = np.random.uniform(0, 5, size=(N,))

plt.bar(x,y, color=rvb(x/N))
plt.show()

这篇关于根据颜色图着色的条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆