繁体   English   中英

更改轮廓第一级的颜色

[英]change color for first level of contourf

我正在使用此脚本来绘制Ramachandran图(这里的图并不真正相关,重要的是输出的图):

#!/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')

这是我得到的:

最终图

那几乎就是我想要的。 我希望第一级(颜色栏上的0-15)以最暗的紫色出现,在图表上显示为白色。

能做到吗?

低于15的白色面罩都能正常工作吗?

from numpy import ma
counts_masked = ma.masked_where(counts<15, counts)
plt.contourf(counts_masked.transpose(), 10, extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()])

或者,另一个选择可能是:

levels = np.linspace(15,165,10) # your colorbar range, starting at 15
cmap = plt.get_cmap()
cmap.set_under(color='white')
plt.contourf(counts.transpose(), levels=levels, cmap=cmap, extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()])

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

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

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)

暂无
暂无

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

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