改变轮廓的第一级别的颜色 [英] change color for first level of contourf

查看:158
本文介绍了改变轮廓的第一级别的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个脚本来绘制一张Ramachandran情节(这种情节在这里并不重要,重要的是输出图):

 #!/ usr / bin / python 
#编码:utf-8


绘制二面角热图的脚本
http://stackoverflow.com/questions/26351621/turn-hist2d-output-into-contours-in-matplotlib


导入sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

#print(sys.argv [1])$ ​​b
$ b frame,x,y = np.loadtxt(sys.argv [1],unpack = True)

fig = plt.figure(1)
ax = plt.subplot(111)


count,ybins,xbins,image = plt.hist2d(x,y,bins = 180,norm = LogNorm())
plt.clf()
plt.contourf(counts.transpose(),10,extent = [xbins.min(),xbins.max(),ybins.min(),ybins.max()])

plt。 colorbar()

fig.set_size_inches(30,20)
plt.savefig(sys.argv [1],bbox_inches ='tight')

以下是我所得到的结果:



这几乎就是我想要的。我想在最黑的紫色上出现的第一层(颜色栏上的0-15)在图上显示为白色。



可以这样做吗?

解决方案

基于以下问题,我在一小时前找到了答案:
Python matplotlib更改超出colorbar范围的值的默认颜色



我只是想做相反的事情,所以我用下面的代码设置了下限:

  cs = plt.contourf(counts.transpose(),10,extent = [xbins.min(),xbins.max(),
ybins.min(),ybins.max()])

#15以下的所有bim被绘制成白色
cs.cmap.set_under('w')
cs.set_clim(15)

cbar = plt .colorbar(cs)


I'm using this script to plot a Ramachandran plot (the kind of plot is not really relevant here, what matters is the outputted graph):

#!/usr/bin/python
# coding: utf-8

"""
Script to plot a heat map of the dihedral angles
http://stackoverflow.com/questions/26351621/turn-hist2d-output-into-contours-in-matplotlib
"""

import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

# print(sys.argv[1])

frame, x, y = np.loadtxt(sys.argv[1], unpack=True)

fig = plt.figure(1)
ax = plt.subplot(111)


counts, ybins, xbins, image = plt.hist2d(x, y, bins=180, norm=LogNorm())
plt.clf()
plt.contourf(counts.transpose(), 10, extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()])

plt.colorbar()

fig.set_size_inches(30, 20)
plt.savefig(sys.argv[1], bbox_inches='tight')

And here is what I obtain:

That's almost what I want. I would like the first level (0-15 on the color bar), which appears in the darkest purple, to appears white on the graph.

Can it be done ?

解决方案

I actually found the answer an hour ago, based on this question: Python matplotlib change default color for values exceeding colorbar range

I just wanted to do the opposite, so I set the low limit with the following code:

cs = plt.contourf(counts.transpose(), 10, extent=[xbins.min(), xbins.max(),
                                                  ybins.min(), ybins.max()])

# All bims under 15 are plotted white
cs.cmap.set_under('w')
cs.set_clim(15)

cbar = plt.colorbar(cs)

这篇关于改变轮廓的第一级别的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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