[英]How do I keep the smaller value in a bar chart in front (when I cannot place the bars side by side)?
I am plotting two sets of data on a chart that has the amino acid position along the x-axis. 我正在图表上绘制两组数据,这些数据具有沿x轴的氨基酸位置。 Sometimes assay 1 as a smaller value and sometimes assay 2 has the smaller value. 有时测定1作为较小的值,有时测定2具有较小的值。 How do I ensure that the lower value is always in front? 如何确保较低的值始终在前面?
I am using python and matplotlib / pyplot. 我正在使用python和matplotlib / pyplot。
Right now I am adding two bar charts as: 现在我添加两个条形图:
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)
Thanks! 谢谢!
You can use the numpy.ma
module to define masks: 您可以使用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)
Example: 例:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.