如何将较小的值保留在前面的条形图中(当无法将条形并排放置时)? [英] How do I keep the smaller value in a bar chart in front (when I cannot place the bars side by side)?

查看:78
本文介绍了如何将较小的值保留在前面的条形图中(当无法将条形并排放置时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在图表上绘制两组数据,这些数据具有沿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',线宽= 0,width = 0.5,log = False)

谢谢!

解决方案

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

 导入numpy.ma作为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',线宽= 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)


I am plotting two sets of data on a chart that has the amino acid position along the x-axis. Sometimes assay 1 as a smaller value and sometimes assay 2 has the smaller value. How do I ensure that the lower value is always in front?

I am using python and 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:

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)

这篇关于如何将较小的值保留在前面的条形图中(当无法将条形并排放置时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆