根据要求在Python/Matplotlib中为热图着色 [英] Color a heatmap in Python/Matplotlib according to requirement

查看:296
本文介绍了根据要求在Python/Matplotlib中为热图着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照指定的颜色制作热图.我想为数据设置一个时间间隔,并判断为正常,然后将其着色为绿色,其余结果应着色为红色.有人知道如何执行此操作吗? 我随附了一个使用pandas和matplotlib的简单示例,以使您更好地理解.

I'm trying to make a heatmap with a specified requirement of the coloring. I want to set an interval for the data and judge that as ok and color it green, the rest of the results should be colored as red. Does anyone have a clue of how to do this?? I have attache a simple example using pandas and matplotlib for better understanding.

import numpy as np 
from pandas import *
import matplotlib.pyplot as plt

Index= ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
Cols = ['A', 'B', 'C', 'D']
data= abs(np.random.randn(5, 4))
df = DataFrame(data, index=Index, columns=Cols)

plt.pcolor(df)
plt.yticks(np.arange(0.5, len(df.index), 1), df.index)
plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
plt.show()

推荐答案

有多种方法可以做到这一点.

There's more than one way to do this.

最简单的方法是将一个布尔数组传递给pcolor,然后选择一个颜色图,其中绿色为高,红色为低.

The easiest way is to just pass in a boolean array to pcolor and then choose a colormap where green is high and red is low.

例如:

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

Index= ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
Cols = ['A', 'B', 'C', 'D']
data= np.random.random((5, 4))
df = pd.DataFrame(data, index=Index, columns=Cols)

plt.pcolor(df > 0.5, cmap='RdYlGn')
plt.yticks(np.arange(0.5, len(df.index), 1), df.index)
plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
plt.show()

或者,如@Cyber​​所述,您可以根据自己的值制作两种颜色的颜色图并使用它:

Alternately, as @Cyber mentioned, you could make a two-color colormap based on your values and use it:

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

Index= ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
Cols = ['A', 'B', 'C', 'D']
data= np.random.random((5, 4))
df = pd.DataFrame(data, index=Index, columns=Cols)

# Values from 0-0.5 will be red and 0.5-1 will be green
cmap, norm = mcolors.from_levels_and_colors([0, 0.5, 1], ['red', 'green'])

plt.pcolor(df, cmap=cmap, norm=norm)
plt.yticks(np.arange(0.5, len(df.index), 1), df.index)
plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
plt.show()

(颜色差异仅是因为"RdYlGn"颜色图使用较深的绿色和红色作为其端点.)

(The color difference is just because the "RdYlGn" colormap uses darker greens and reds as its endpoints.)

另一方面,为此使用pcolormesh而不是pcolor也要快得多.对于小型阵列,这不会产生显着差异,但是对于大型阵列,pcolor的速度过慢.如果您不介意栅格输出,则imshow甚至更快.使用imshow(data, interpolation='nearest', aspect='auto', origin='lower')匹配pcolorpcolormesh的默认值.

On a side note, it's also considerably faster to use pcolormesh for this, rather than pcolor. For small arrays, it won't make a significant difference, but for large arrays pcolor is excessively slow. imshow is even faster yet, if you don't mind raster output. Use imshow(data, interpolation='nearest', aspect='auto', origin='lower') to match the defaults of pcolor and pcolormesh.

这篇关于根据要求在Python/Matplotlib中为热图着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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