2

我在一张图表上绘制了两组数据,该图表具有沿 x 轴的氨基酸位置。有时测定 1 的值较小,有时测定 2 的值较小。如何确保较低的值始终在前面?

我正在使用 python 和 matplotlib / pyplot。

现在我正在添加两个条形图:

p1 = plot.bar(x1, y1, color='r', alpha=1, edgecolor='none',linewidth=0,width=0.5, log=False)
p2 = plot.bar(x2, y2, color='b', alpha=1, edgecolor='none', linewidth=0,width=0.5, log=False)

谢谢!

4

1 回答 1

7

您可以使用该numpy.ma模块来定义掩码:

import numpy.ma as ma

mask1 = ma.where(y1>=y2)
mask2 = ma.where(y2>=y1)

p1 = plt.bar(x1[mask1], y1[mask1], color='r', alpha=1, edgecolor='none',linewidth=0,width=0.5, log=False)
p2 = plt.bar(x2, y2, color='b', alpha=1, edgecolor='none', linewidth=0,width=0.5, log=False)
p3 = plt.bar(x1[mask2], y1[mask2], color='r', alpha=1, edgecolor='none',linewidth=0,width=0.5, log=False)

例子:

import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma

x1 = x2 = np.arange(5)
y1 = np.array([1,4,25,2,4])
y2 = np.array([4,2,3,32,6])

mask1 = ma.where(y1>=y2)
mask2 = ma.where(y2>=y1)

p1 = plt.bar(x1[mask1], y1[mask1], color='r', alpha=1, edgecolor='none',linewidth=0,width=0.5, log=False)
p2 = plt.bar(x2, y2, color='b', alpha=1, edgecolor='none', linewidth=0,width=0.5, log=False)
p3 = plt.bar(x1[mask2], y1[mask2], color='r', alpha=1, edgecolor='none',linewidth=0,width=0.5, log=False)

在此处输入图像描述

于 2013-07-19T15:47:23.093 回答