Matplotlib:调整刻度以适应图中的大小 [英] Matplotlib: Adjusting the ticks to fit within the figure

查看:262
本文介绍了Matplotlib:调整刻度以适应图中的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下matplotlib代码,它所做的只是在x轴上绘制0-20,而在y轴上绘制0-100

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(20))
ax.set_yticks(range(100))
labels = ax.set_yticklabels(range(100))

我遇到的问题是,并非所有标签实际上都适合y轴.

我要做的是将最大标签尺寸除以图形的总面积,以计算可以放入的标签数量,这样我就可以将数据分为0-10、10-20、30- 40并绘制这些图形,而不是显示单个标签1,2,3,4,5,6,7,8 ...

我在查找标签的标签大小和图形的标签大小时遇到​​麻烦(如果我写错了,请纠正我,也许是像素的轴大小.这是我第一次使用matplotlib)

如何找出图形/轴占用的像素数(宽度+高度)以及每个标签的像素数(宽度+高度)?

解决方案

您可以使用ax.get_position()获取绘图中的轴范围,请参见axes上的文档(我会发布一个链接,但我目前的声誉是不允许).

刻度标签大小是在matplotlibrc文件中相对于字体大小定义的.在此处中查看一些详细信息.默认大小为12pt,可以使用

进行更改

plt.rcParams['ytick.labelsize'] = small

从标准matplotibrc文件中:

可以定义特殊的文字大小 相对于font.size,使用以下值:xx-small,x-small, 小,中,大,x大,xx大,大或小

解决问题的最简单方法可能就是通过使用来更改总图形大小

fig = plt.figure(figsize=(10,30))

,并根据您的情况提供以下结果(请注意,示例有点极端).如果刻度标签太多,那么您就必须增加图形总大小,那么您应该考虑像上面显示的那样减小刻度标签的大小,或者减少刻度标签的数量.

请注意,此过程涉及手动调整总图和/或刻度标签的大小,直到获得所需的输出为止.如果有更好的方法,我将很高兴看到它.

您可以使用plt.savefig(output.png)将图保存在当前工作目录中.如果使用plt.show()并且图像大于弹出窗口,则标签将始终被弄乱,因为总图形会缩小到窗口大小,但标签大小保持不变.

我希望这个答案对您有帮助.

I have the following matplotlib code which all it does is plots 0-20 on the x-axis vs 0-100 on the y-axis

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(20))
ax.set_yticks(range(100))
labels = ax.set_yticklabels(range(100))

The problem I have is that not all the labels actually fit within the y-axis.

What I want to do is to divide the maximum label size into the total area of the figure to calculate how many labels I can put so that then I can group my data into sections 0-10, 10-20, 30-40 and plot those rather than display individual labels 1,2,3,4,5,6,7,8...

I am having trouble with finding the label size of the labels and the label size of the figure (correct me if I am wrong maybe it is the axis size in pixels. It is the first time I am using matplotlib)

How can I find out the number of pixels the figure/axes is taking up (width + height) and also the number of pixels (width + height) of each label?

解决方案

You could obtain the axis extents in your plot with ax.get_position(), see the documentation on axes (I would post a link, but my current reputation does not allow it).

The tick labelsize is defined in your matplotlibrc file relative to your font size. See some detail here. The default size is 12pt and can be changed with

plt.rcParams['ytick.labelsize'] = small

From the standard matplotibrc file:

Special text sizes can be defined relative to font.size, using the following values: xx-small, x-small, small, medium, large, x-large, xx-large, larger, or smaller

The easiest way to solve your problem is probably to just change the total figure size by using

fig = plt.figure(figsize=(10,30))

with the following result in your case (note that the example is a little bit extreme). If there are so many ticklabels that you have to increase the total figure size so much, you should consider decreasing the ticklabel size like shown above or decrease the number of ticklabels.

Note that this procedure involves manually adjusting the size of your total plot and/or your ticklabels until you obtain your desired output. If there is some better way to do it, I would be pleased to see it.

You can save your plot with plt.savefig(output.png) in your current working directory. If you use plt.show() and the image is larger than the popup-window, the labels will always be messed up, because the total figure is shrinked down to the window size but the label size stays constant.

I hope this answer is helpful for you.

这篇关于Matplotlib:调整刻度以适应图中的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆