Zho*_*gyu 3 python plot matplotlib
如何使用python和matplotlib绘制如下图片?我知道如何绘制2D热图,但是通过绘制热图顶部的条形图以及颜色条和热图之间的条形图,它让我感到很沮丧.如何在图片上添加这两个条,并在x轴或y轴上显示数字属于哪个组?
非常感谢所有的回复.
虽然在一开始就有点麻烦,但系统而直接的方法是使用matplotlib.gridspec.GridSpec.
首先设置网格:
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
fig = plt.figure()
gs = GridSpec(2, 3, width_ratios=[10, 1, 1], height_ratios=[1, 10])
Run Code Online (Sandbox Code Playgroud)
这给了我们一个2行3列的网格,其中左下轴为10x10,其他轴的相对大小为10x1或1x10.这些比例可以根据自己的喜好进行调整.请注意,顶部中心/右轴将为空.
big_ax = fig.add_subplot(gs[1,0]) # bottom left
top_ax = fig.add_subplot(gs[0,0]) # top left
right_ax = fig.add_subplot(gs[1,1]) # bottom center
cbar_ax = fig.add_subplot(gs[1,2]) # bottom right
Run Code Online (Sandbox Code Playgroud)
并将生成随机热图.我使用的imshow(aspect='auto')是图像对象和热图占据各自轴的整个空间(否则它们将覆盖由gridspec设置的高度/宽度比).
im = plt.imread('/path/to/image.png')
# Plot your heatmap on big_ax and colorbar on cbar_ax
heatmap = big_ax.imshow(np.random.rand(10, 10), aspect='auto', origin='lower')
cbar = fig.colorbar(heatmap, cax=cbar_ax)
# Show your images on top_ax and right_ax
top_ax.imshow(im, aspect='auto')
# need to rotate my image.
# you may not have to if you have two different images
from scipy import ndimage
right_ax.imshow(ndimage.rotate(im, 90), aspect='auto')
# Clean up the image axes (remove ticks, etc.)
right_ax.set_axis_off()
top_ax.set_axis_off()
# remove spacing between axes
fig.subplots_adjust(wspace=0.05, hspace=0.05)
Run Code Online (Sandbox Code Playgroud)
它不是超级迷人(特别是默认的喷射色彩映射),但你可以轻松地使用它来重现你的OP的数字.
编辑: 所以如果你想在顶部和右边生成类似基因组的情节,你可以尝试这样的顶部栏:
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
# draw the black line
top_ax.axhline(0, color='k', zorder=-1)
# box x-coords and text labels
boxes = zip(np.arange(0.1, 1, 0.2), np.arange(0.2, 1, 0.2))
box_text = ('A1', 'B1', 'B2', 'A2')
# color indicators for boxes
colors = (0, 1, 1, 0)
# construct Rects
patches = [Rectangle(xy=(x0, -1), width=(x1-x0), height=2) for x0,x1 in boxes]
p = PatchCollection(patches, cmap='jet')
# this maps the colors in [0,1] to the cmap above
p.set_array(np.array(colors))
top_ax.add_collection(p)
# add text
[top_ax.text((x0+x1)/2., 1.2, text, ha='center')
for (x0,x1), text in zip(boxes, box_text)]
# adjust ylims
top_ax.set_ylim(-2, 2)
Run Code Online (Sandbox Code Playgroud)
对于正确的轴,您可以做同样的事情,但使用axvline并交换x-coords用于y-coords.
right_ax.axvline(0, color='k', zorder=-1)
patches = [Rectangle(xy=(-1, y0), width=2, height=(y1-y0)) for y0, y1 in boxes]
p = PatchCollection(patches, cmap='jet')
p.set_array(np.array(colors))
right_ax.add_collection(p)
[right_ax.text(1.2, (y0+y1)/2., text, va='center')
for (y0, y1), text in zip(boxes, box_text)]
right_ax.set_xlim(-2,2)
Run Code Online (Sandbox Code Playgroud)
这些修改导致类似于:
| 归档时间: |
|
| 查看次数: |
1346 次 |
| 最近记录: |