如何在前面的条形图中保留较小的值(当我不能并排放置条形图时)?

时间:2013-07-19 15:25:49

标签: python charts matplotlib

我正在图表上绘制两组数据,这些数据具有沿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)

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用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)

enter image description here