以一种美观的方式绘制43个不同类别的频率

我根据训练集中每个类别的频率创建了以下直方图

enter image description here

每个类别的标签太长,类似于

Speed limit (20km/h)

我可以将每个标签放在条形上吗?


回答:

也许像这样?

enter image description here

import numpy as npimport matplotlib.pyplot as pltN=5xlabel = ["Speed limit ("+str(i)+"km/h)" for i in range(0,N)]xs = np.arange(0,7,1.5)ys = [8,6,10,7,9]width = 0.3*np.ones(N)fig, ax = plt.subplots()bars = ax.bar(xs, ys, width, color='k',alpha=0.3)plt.xticks(xs, xlabel,rotation=270)for i,bar in enumerate(bars):    height = bar.get_height()    ax.text(bar.get_x() + bar.get_width()/2., 0.1*height,                '%s' % xlabel[i],rotation=90,ha='center', va='bottom')plt.show()

将其更改为水平条形图:

enter image description here

import numpy as npimport matplotlib.pyplot as pltN = 5xlabel = ["Speed limit ("+str(i)+"km/h)" for i in range(0,5)]xs = np.arange(0,5)/2ys = [8,6,10,7,9]width = 0.3*np.ones(N)fig, ax = plt.subplots()bars = ax.barh(xs, ys, width, color='k',alpha=0.3)plt.xticks([])for i,bar in enumerate(bars):    height = bar.get_height()    ax.text(bar.get_x()+3, bar.get_y()+bar.get_height()/3,                '%s' % xlabel[i],rotation=0,ha='center', va='bottom')plt.tight_layout()plt.show()

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注