简体   繁体   English

使用matplotlib在条形图中反映点

[英]Reflecting points in a bar graph using matplotlib

long time lurker for programming questions, first time poster. 长时间潜伏编程问题,第一次海报。

I am writing some code where I am making a bar graph of a bunch of values, some of which are negative and some of which are positive- plot here 我正在写一些代码,我正在制作一堆值的条形图,其中一些是负数,其中一些是正数 - 这里情节

In short, what I want to do is take all the negative values for the green part and overlay them onto the positive side, so you can see the asymmetry in those values. 简而言之,我想要做的是获取绿色部分的所有负值并将它们叠加到正面,这样您就可以看到这些值的不对称性。 I have tried a few methods to get this to work, and perhaps I'm not searching the correct things but can't seem to find a good answer on how to do this. 我已经尝试了一些方法来实现这一点,也许我没有找到正确的东西,但似乎无法找到一个如何做到这一点的好答案。

The relevant code I have so far (hopefully not leaving anything out that's important for the purposes of the plot...): 到目前为止我所拥有的相关代码(希望不会留下任何对于情节目的而言重要的东西......):

import glob
import pyrap.images as pim
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
import matplotlib.mlab as mlab
from scipy.optimize import * 

less_than_expected_min = -500
more_than_expected_max = 1200
n_bins = 100

bin_edges = np.linspace(less_than_expected_min, more_than_expected_max, n_bins)
for i in range(total):
    all_clipped_values = np.zeros([total,n_bins-1])

clipped_levels= np.sum(all_clipped_values,axis=0)

reflect= np.concatenate([clipped_levels[0:30], clipped_levels[30:0]])


plt.bar(bin_edges[:-1],clipped_levels,width=(more_than_expected_max -less_than_expected_min)/float(n_bins), log=True,color='green')
plt.bar(bin_edges[:-1],reflect,width=(more_than_expected_max -less_than_expected_min)/float(n_bins), log=True,color='red')

The issue when I try this method, however, is I get "AssertionError: incompatible sizes: argument 'height' must be length 99 or scalar." 但是,当我尝试这种方法的问题是,我得到“AssertionError:不兼容的大小:参数'高度'必须是长度99或标量。” It's not quite clear to me how to solve this, or in fact if there is a simpler way to do this reflection than what I'm thinking. 我不太清楚如何解决这个问题,或者事实上,如果有一种比我想的更简单的方法来做这种反思。

Any feedback appreciated- thanks! 任何反馈意见 - 谢谢!

As I mentioned in my comment, maybe this will clarify 正如我在评论中提到的,也许这会澄清

>>> x = list(range(100))
>>> x[0:30]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
>>> x[30:0]
[]
>>> x[30:0:-1]
[30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

You have to specify the negative step. 您必须指定否定步骤。

Each time you call plt.bar , if your first argument is an array of length n (the set of abscissas), then your second argument must be an array of length n (the set of ordinates). 每次调用plt.bar ,如果你的第一个参数是长度为n的数组( plt.bar的集合),那么你的第二个参数必须是长度为n的数组(纵坐标数组)。

In your case, your set of abscissas is by construction an array of length 99, hence you must ensure your set of ordinates has the same shape. 在您的情况下,您的横坐标是构造一个长度为99的数组,因此您必须确保您的纵坐标具有相同的形状。

For the first call, your second argument clipped_levels seems to have the right length, but for the second call, the second argument is reflect - which is far from 99 items long. 对于第一个调用,你的第二个参数clipped_levels似乎具有正确的长度,但对于第二个调用,第二个参数是reflect - 这远远超过99个项目。

Fix that and it should work, hopefully ! 修复它,它应该工作,希望!

EDIT : 编辑:

Something like reverse = np.concatenate([clipped_levels[:n_bins/2], clipped_levels[n_bins/2-2::-1]]) should do the trick. reverse = np.concatenate([clipped_levels[:n_bins/2], clipped_levels[n_bins/2-2::-1]])这样的东西应该可以解决问题。

Also, I still think your for loop could be replaced by a single instruction (the initialization of all_clipped_values ), unless there is some other code inside that was not relevant here. 此外,我仍然认为你的for循环可以被单个指令( all_clipped_values的初始化) all_clipped_values ,除非里面有一些其他代码与此无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM