简体   繁体   English

如何确保matplotlib图的x轴上的标签之间的间距均匀?

[英]How to ensure even spacing between labels on x axis of matplotlib graph?

I have been given a data for which I need to find a histogram . 我得到的数据需要查找histogram So I used pandas hist() function and plot it using matplotlib . 因此,我使用了熊猫hist()函数,并使用matplotlib对其进行了绘制。 The code runs on a remote server so I cannot directly see it and hence I save the image. 该代码在远程服务器上运行,因此我无法直接看到它,因此保存了图像。 Here is what the image looks like 这是图像的样子

在此处输入图片说明

Here is my code below 这是我的下面的代码

import matplotlib.pyplot as plt

df_hist = pd.DataFrame(np.array(raw_data)).hist(bins=5) // raw_data is the data supplied to me
plt.savefig('/path/to/file.png')
plt.close()

As you can see the x axis labels are overlapping. 如您所见,x轴标签重叠。 So I used this function plt.tight_layout() like so 所以我像这样使用了函数plt.tight_layout()

import matplotlib.pyplot as plt

df_hist = pd.DataFrame(np.array(raw_data)).hist(bins=5)
plt.tight_layout()
plt.savefig('/path/to/file.png')
plt.close()

There is some improvement now 现在有一些改善

在此处输入图片说明

But still the labels are too close. 但是标签仍然太近了。 Is there a way to ensure the labels do not touch each other and there is fair spacing between them? 有没有办法确保标签彼此不接触并且标签之间有合理的间距? Also I want to resize the image to make it smaller. 我也想调整图像大小以使其更小。

I checked the documentation here https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html but not sure which parameter to use for savefig . 我在这里查看了文档https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html,但不确定要为savefig使用哪个参数。

Since raw_data is not already a pandas dataframe there's no need to turn it into one to do the plotting. 由于raw_data尚未成为pandas数据框,因此无需将其转换为一个即可进行绘制。 Instead you can plot directly with matplotlib. 相反,您可以直接使用matplotlib进行绘制。

There are many different ways to achieve what you'd like. 有多种方法可以实现您想要的。 I'll start by setting up some data which looks similar to yours: 我将从设置一些与您的数据相似的数据开始:

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import gamma

raw_data = gamma.rvs(a=1, scale=1e6, size=100)

If we go ahead and use matplotlib to create the histogram we may find the xticks too close together: 如果我们继续使用matplotlib创建直方图,我们可能会发现xticks过于靠近:

fig, ax = plt.subplots(1, 1, figsize=[5, 3])
ax.hist(raw_data, bins=5)
fig.tight_layout()

在此处输入图片说明

The xticks are hard to read with all the zeros, regardless of spacing. xticks很难读取所有零,而与间距无关。 So, one thing you may wish to do would be to use scientific formatting. 因此,您可能希望做的一件事就是使用科学格式。 This makes the x-axis much easier to interpret: 这使x轴更易于解释:

ax.ticklabel_format(style='sci', axis='x', scilimits=(0,0))

在此处输入图片说明

Another option, without using scientific formatting would be to rotate the ticks (as mentioned in the comments): 不使用科学格式的另一种选择是旋转刻度线(如评论中所述):

ax.tick_params(axis='x', rotation=45)
fig.tight_layout()

在此处输入图片说明

Finally, you also mentioned altering the size of the image. 最后,您还提到了更改图像的大小。 Note that this is best done when the figure is initialised. 请注意,最好在初始化图形时完成此操作。 You can set the size of the figure with the figsize argument. 您可以使用figsize参数设置图形的大小。 The following would create a figure 5" wide and 3" in height: 下面将创建一个5英寸宽,3英寸高的图形:

fig, ax = plt.subplots(1, 1, figsize=[5, 3])

I think the two best fixes were mentioned by Pam in the comments. 我认为Pam在评论中提到了两个最佳解决方案。 You can rotate the labels with plt.xticks(rotation=45 For more information, look here: Rotate axis text in python matplotlib 您可以使用plt.xticks(rotation = 45旋转标签。有关更多信息,请参见此处: 旋转python matplotlib中的轴文本

The real problem is too many zeros that don't provide any extra info. 真正的问题是太多的零没有提供任何额外的信息。 Numpy arrays are pretty easy to work with, so pd.DataFrame(np.array(raw_data)/1000).hist(bins=5) should get rid of three zeros off of both axes. Numpy数组非常易于使用,因此pd.DataFrame(np.array(raw_data)/1000).hist(bins=5)应该摆脱两个轴上的三个零。 Then just add a 'kilo' in the axes labels. 然后只需在轴标签中添加一个“千”。

To change the size of the graph use rcParams. 要更改图形的大小,请使用rcParams。

from matplotlib import rcParams rcParams['figure.figsize'] = 7, 5.75 #the numbers are the dimensions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM