python - Matplotlib 直方图标签文本拥挤
问题描述
我在 matplotlib 中制作直方图,每个 bin 的文本标签相互重叠,如下所示:
我尝试通过遵循另一个解决方案来旋转 x 轴上的标签
cuisine_hist = plt.hist(train.cuisine, bins=100)
cuisine_hist.set_xticklabels(rotation=45)
plt.show()
但我收到错误消息'tuple' object has no attribute 'set_xticklabels'
。为什么?我该如何解决这个问题?或者,如何“转置”绘图以使标签位于垂直轴上?
解决方案
干得好。我在一个例子中将这两个答案混为一谈:
# create figure and ax objects, it is a good practice to always start with this
fig, ax = plt.subplots()
# then plot histogram using axis
# note that you can change orientation using keyword
ax.hist(np.random.rand(100), bins=10, orientation="horizontal")
# get_xticklabels() actually gets you an iterable, so you need to rotate each label
for tick in ax.get_xticklabels():
tick.set_rotation(45)
推荐阅读
- tableau-api - 在 Tableau 中显示或隐藏图表
- python - 如何使用 django 加快 PostgreSQL 数据库中的正则表达式查询
- spring-boot - 如何在 Kafka 消费者重试用尽时设置确认
- javascript - 如何使 Cytoscape.js 画布尊重其父级的位置
- spfx - 如何在应用页面上编辑 webpart 的属性
- javascript - React Native Android 构建,“错误:无法获取未知属性 'mergeResourcesProvider'”
- c# - 如何判断一个coroutine是否已经播放?
- hadoop - 确认重写查询
- c# - 在 xamarin.forms 中使用 fontawesome(仅限 iOS 和 Android)
- javascript - 如何使整行可点击角度4