我正在使用这个剧本绘制一张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)显示为最暗的紫色,在图表上显示为白色。
可以吗?
答案 0 :(得分:1)
面具是否有效,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()])