我想有效地绘制一些数据以便于可视化,但是遇到了一些困难。我有两个值。一个是离散的(0或1),称为
然后,根据标签是1还是0来分割Y轴,因此我们得到这样的图:
如果有任何有效的、智能的方法来拆分我的数据(而不仅是为这些值硬编码四个柱形图),我也会感兴趣,尽管这可能需要另一个问题。当我有代码运行时,我会发布它。
我将这两个值存储在numpy数组中,如下所示,但不确定如何绘制这样的图表:
任何帮助都非常感激,谢谢。 编辑: 我现在遇到了这个错误:
label
。另一个是连续值,范围在0和1之间。我希望创建一个直方图,在X轴上有许多条柱形图,例如每个.25的数据一个柱形图,因此有四个柱子,第一个柱子的值为0-0.25,第二个为0.25-0.5,第三个为0.5-0.75,第四个为0.75-1。然后,根据标签是1还是0来分割Y轴,因此我们得到这样的图:
如果有任何有效的、智能的方法来拆分我的数据(而不仅是为这些值硬编码四个柱形图),我也会感兴趣,尽管这可能需要另一个问题。当我有代码运行时,我会发布它。
我将这两个值存储在numpy数组中,如下所示,但不确定如何绘制这样的图表:
import numpy as np
import pylab as P
variable_values = trainData.get_vector('variable') #returns one dimensional numpy array of vals
label_values = trainData.get_vector('label')
x = alchemy_category_score_values[alchemy_category_score_values != '?'].astype(float) #removing void vals
y = label_values[alchemy_category_score_values != '?'].astype(float)
fig = plt.figure()
plt.title("Feature breakdown histogram")
plt.xlabel("Variable")
plt.xlim(0, 1)
plt.ylabel("Label")
plt.ylim(0, 1)
xvals = np.linspace(0,1,.02)
plt.show()
Matplotlib教程展示了以下代码,大致实现了我想要的功能,但我无法真正理解它是如何工作的(链接):
P.figure()
n, bins, patches = P.hist(x, 10, normed=1, histtype='bar', stacked=True)
P.show()
任何帮助都非常感激,谢谢。 编辑: 我现在遇到了这个错误:
AssertionError: incompatible sizes: argument 'height' must be length 5 or scalar
我已经打印出了两个numpy数组,它们的长度相等,一个是离散的,另一个是连续的。这是我正在运行的代码:
x = variable_values[variable_values != '?'].astype(float)
y = label_values[label_values != '?'].astype(float)
print x #printing numpy arrays of equal size, x is continuous, y is discrete. Both of type float now.
print y
N = 5
ind = np.arange(N) # the x locations for the groups
width = 0.45 # the width of the bars: can also be len(x) sequence
p1 = plt.bar(ind, y, width, color='r') #error occurs here
p2 = plt.bar(ind, x, width, color='y',
bottom=x)
plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind+width/2., ('G1', 'G2', 'G3', 'G4', 'G5') )
plt.yticks(np.arange(0,81,10))
plt.legend( (p1[0], p2[0]), ('Men', 'Women') )
plt.show()
x = mu + sigma*P.randn(1000,3)
?这用于制作三个堆叠的条形图。 - Aris F.N
,它是直方图中条形的数量。请写入 4 或使用len(x)
。 - logc