python绘制多个条形图_python – 在Matplotlib中绘制多个直方图 – 颜色或并排条形图...

博主在尝试使用Matplotlib在同一图形上绘制三个不同数组的直方图,以检测数据不平衡。遇到的问题包括:直方图无法区分,左侧标签“计数”超出图像范围。在尝试并排显示直方图和调整标签位置后,代码已有所改进,但左侧标签仍然部分超出。目前代码能够正常工作,但作者希望进一步优化,使直方图的标签更加居中。
摘要由CSDN通过智能技术生成

问题:在Matplotlib中绘制多个直方图时,我无法区分绘图与另一个绘图

图像问题:** xe2Zr.png

**次要问题:部分左侧标签“计数”不在图像范围内.为什么?

描述

我想绘制3个不同组的直方图.每组都是一个0和1的数组.我想要每个的直方图,所以我可以检测数据集上的不平衡.

我让它们分开绘制,但我想要一起绘制它们的图形.

可以并排显示不同的图形,或者我甚至用谷歌搜索将其绘制为3D,但我不知道在图形上“阅读”或“查看”并理解它是多么容易.

现在,我想在同一图形的每一侧绘制[train],[validation]和[test]条形图,如下所示:

PvJcD.png

PS:我的谷歌搜索没有返回任何可以理解的代码.

此外,我想如果有人会检查我是否对我的代码做了任何疯狂.

非常感谢!

代码:

def generate_histogram_from_array_of_labels(Y=[], labels=[], xLabel="Class/Label", yLabel="Count", title="Histogram of Trainset"):

plt.figure()

plt.clf()

colors = ["b", "r", "m", "w", "k", "g", "c", "y"]

information = []

for index in xrange(0, len(Y)):

y = Y[index]

if index > len(colors):

color = colors[0]

else:

color = colors[index]

if labels is None:

label = "?"

else:

if index < len(labels):

label = labels[index]

else:

label = "?"

unique, counts = np.unique(y, return_counts=True)

unique_count = np.empty(shape=(unique.shape[0], 2), dtype=np.uint32)

for x in xrange(0, unique.shape[0]):

unique_count[x, 0] = unique[x]

unique_count[x, 1] = counts[x]

information.append(unique_count)

# the histogram of the data

n, bins, patches = plt.hist(y, unique.shape[0], normed=False, facecolor=color, alpha=0.75, range=[np.min(unique), np.max(unique) + 1], label=label)

xticks_pos = [0.5 * patch.get_width() + patch.get_xy()[0] for patch in patches]

plt.xticks(xticks_pos, unique)

plt.xlabel(xLabel)

plt.ylabel(yLabel)

plt.title(title)

plt.grid(True)

plt.legend()

# plt.show()

string_of_graphic_image = cStringIO.StringIO()

plt.savefig(string_of_graphic_image, format='png')

string_of_graphic_image.seek(0)

return base64.b64encode(string_of_graphic_image.read()), information

编辑

在哈希码的答案之后,这个新代码:

def generate_histogram_from_array_of_labels(Y=[], labels=[], xLabel="Class/Label", yLabel="Count", title="Histogram of Trainset"):

plt.figure()

plt.clf()

colors = ["b", "r", "m", "w", "k", "g", "c", "y"]

to_use_colors = []

information = []

for index in xrange(0, len(Y)):

y = Y[index]

if index > len(colors):

to_use_colors.append(colors[0])

else:

to_use_colors.append(colors[index])

unique, counts = np.unique(y, return_counts=True)

unique_count = np.empty(shape=(unique.shape[0], 2), dtype=np.uint32)

for x in xrange(0, unique.shape[0]):

unique_count[x, 0] = unique[x]

unique_count[x, 1] = counts[x]

information.append(unique_count)

unique, counts = np.unique(Y[0], return_counts=True)

histrange = [np.min(unique), np.max(unique) + 1]

# the histogram of the data

n, bins, patches = plt.hist(Y, 1000, normed=False, alpha=0.75, range=histrange, label=labels)

#xticks_pos = [0.5 * patch.get_width() + patch.get_xy()[0] for patch in patches]

#plt.xticks(xticks_pos, unique)

plt.xlabel(xLabel)

plt.ylabel(yLabel)

plt.title(title)

plt.grid(True)

plt.legend()

产生这个:

9gO4o.png

– 新编辑:

def generate_histogram_from_array_of_labels(Y=[], labels=[], xLabel="Class/Label", yLabel="Count", title="Histogram of Trainset"):

plt.figure()

plt.clf()

information = []

for index in xrange(0, len(Y)):

y = Y[index]

unique, counts = np.unique(y, return_counts=True)

unique_count = np.empty(shape=(unique.shape[0], 2), dtype=np.uint32)

for x in xrange(0, unique.shape[0]):

unique_count[x, 0] = unique[x]

unique_count[x, 1] = counts[x]

information.append(unique_count)

n, bins, patches = plt.hist(Y, normed=False, alpha=0.75, label=labels)

plt.xticks((0.25, 0.75), (0, 1))

plt.xlabel(xLabel)

plt.ylabel(yLabel)

plt.title(title)

plt.grid(True)

plt.legend()

现在正在工作,但是,左侧的标签有点出界,我想更好地使酒吧居中……我怎么能这样做?

结果:FNPnh.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值