如何使用matplotlib绘制中心值不为零的垂直直方图

时间:2018-04-06 15:59:04

标签: python matplotlib

我使用以下代码绘制一些数据:

self.arms = df['arms'].iloc[-self.plotlength:]
self.armsup = [0 if i < 1.0 else log(i, 10) for i in self.arms]
self.armsdn = [0 if i > 1.0 else log(i, 10) for i in self.arms]

a1.plot(self.x, self.openarms - 1, color='k')
a1.vlines(self.x, 0, self.armsup, color='g')
a1.vlines(self.x, 0, self.armsdn, color='r')

具有以下结果(下方图表剪辑) enter image description here

直方图目前以零为中心。它应该以一个为中心。目前我正在通过减去一个来调整数据。如何绘制在一个任意数字之上和之下摆动的垂直线?

1 个答案:

答案 0 :(得分:2)

看起来你正在记录self.arms中的每个值,并且你知道中值应该是1.0左右。而不是

self.arms = df['arms'].iloc[-self.plotlength:]
self.armsup = [0 if i < 1.0 else log(i, 10) for i in self.arms]
self.armsdn = [0 if i > 1.0 else log(i, 10) for i in self.arms]

你可以使用

self.arms = df['arms'].iloc[-self.plotlength:]
self.armsup = [1.0 if log(i, 10) < 1.0 else log(i, 10) for i in self.arms]
self.armsdn = [1.0 if log(i, 10) > 1.0 else log(i, 10) for i in self.arms]

或类似地,使用NumPy:

self.arms = df['arms'].iloc[-self.plotlength:]
log_arms = np.log(self.arms)
threshold = 1
mask = log_arms > threshold
armsup = np.where(mask, log_arms, threshold)
armsdn = np.where(~mask, log_arms, threshold)

np.where有调用签名np.where(cond, x, y),其中cond是布尔数组,xy是与cond长度相同的数组,np.where(cond, x, y)返回x cond为真的ycond import numpy as np import matplotlib.pyplot as plt N = 100 arms = np.random.uniform(0.5, 4.0, N) log_arms = np.log(arms) threshold = 1 mask = log_arms > threshold armsup = np.where(mask, log_arms, threshold) armsdn = np.where(~mask, log_arms, threshold) fig, ax = plt.subplots() x = np.arange(N) ax.vlines(x, threshold, armsup, color='g') ax.vlines(x, armsdn, threshold, color='r') plt.show() 为假的值。

例如,

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

的产率 enter image description here