简体   繁体   English

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

[英]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.

相关问题 我将如何像 Altair 这样并排创建条形图? - How would I create a bar chart side by side like this Altair? 如何并排放置两个或多个 ASCII 图像? - How do I place two or more ASCII images side by side? 如何在 tkinter 中将 label 文本与 label 值并排保存 - How do I keep label text side by side with label value in tkinter 如何获取 matplotlib 条形图中的所有条形? - How do I get all bars in a matplotlib bar chart? 如何为条形图中的不同条形提供十六进制颜色? - How do I give a hexadecimal color to different bars in a bar chart? 如何更改此条形图上条形的顺序? - How do I change the order of the bars on this bar chart? 如何在Pygal的堆积条形图中为条形添加值? - How do I add values to the bars in a Stacked Bar chart in Pygal? 我在matplotlib(python)中有一个带有误差条的条形图,但我希望误差条位于该条的中间。 我该怎么做呢? 谢谢 - I have a bar chart with error bars in matplotlib (python), but i want the error bars in the middle of the bar. how do i do this? Thanks 我需要有关如何在以下条形图中将绿色条移动到蓝色条左侧的指导 - I need guidance on how to move the green bar to the left side of the blue bar in the following bar chart Python:如何使用正则表达式拆分列中的值并保留右侧? - Python: How do I split a value in column using regex and keep the right side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM