rotating xticks causes the ticks partially hidden in matplotlib(旋转 xticks 导致刻度部分隐藏在 matplotlib 中)
问题描述
我正在创建一个绘图,其中 x 轴上的名称和 y 轴上的时间值(分钟).x 轴上的名称就像
I am creating a plot with names on x axis and time values(minutes) on y axis.The names on x axis are like
['cooking']18:15:27 ,['study']18:09:19,['travel']18:21:34` etc ..
其中 y 值为 5、1、1 等.我将 xlabel 设为类别",将 ylabel 设为持续时间(以分钟为单位)".
where as the y values are 5,1,1 etc.I have given xlabel as 'categories' and ylabel as 'durations in minutes'.
由于 xticks 是一些长度的字符串,我决定将它们旋转 90 以避免重叠.现在,刻度被部分隐藏并且 xlabel 消失了.有什么办法可以让整个情节适应一切..?
Since the xticks were strings of some length,I decided to rotate them by 90 to avoid overlapping.Now ,the ticks are partially hidden and the xlabel has disappeared. Is there some way I can make the whole plot accommodate everything..?
谢谢
标记
这里是代码片段
import matplotlib.pyplot as plt
...
figure = plt.figure()
barwidth = 0.25
ystep = 10
plt.grid(True)
plt.xlabel('categories')
plt.ylabel('durations in minutes')
plt.title('durations for categories-created at :'+now)
plt.bar(xdata, ydata, width=barwidth,align='center')
plt.xticks(xdata,catnames,rotation=90)
plt.yticks(range(0,maxduration+ystep,ystep))
plt.xlim([min(xdata) - 0.5, max(xdata) + 0.5])
plt.ylim(0,max(ydata)+ystep)
figure.savefig("myplot.png",format="png")
推荐答案
一个不错的选择是旋转刻度标签.
One good option is to rotate the tick labels.
在您的特定情况下,您可能会发现使用 figure.autofmt_xdate()
很方便(这将旋转 x 轴标签等).
In your specific case, you might find it convenient to use figure.autofmt_xdate()
(Which will rotate the x-axis labels among other things).
或者,您可以执行 plt.setp(plt.xticks()[1], rotation=30)
(或执行相同操作的各种其他方式).
Alternatively, you could do plt.setp(plt.xticks()[1], rotation=30)
(or various other ways of doing the same thing).
此外,作为几年后的编辑,使用最新版本的 matplotlib,您可以调用 fig.tight_layout()
来调整大小以适应图中的标签,如下面的@elgehelge 注释.
Also, as a several year later edit, with recent versions of matplotlib, you can call fig.tight_layout()
to resize things to fit the labels inside the figure, as @elgehelge notes below.
这篇关于旋转 xticks 导致刻度部分隐藏在 matplotlib 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:旋转 xticks 导致刻度部分隐藏在 matplotlib 中


基础教程推荐
- 症状类型错误:无法确定关系的真值 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01