【发布时间】:2019-03-05 09:35:52
【问题描述】:
我得到了一个数据,我需要找到一个histogram。所以我使用了 pandas hist() 函数并使用 matplotlib 绘制它。该代码在远程服务器上运行,因此我无法直接看到它,因此我保存了图像。这是图片的样子
下面是我的代码
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()
如您所见,x 轴标签是重叠的。所以我像这样使用了这个函数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()
现在有一些改进
但标签仍然太近了。有没有办法确保标签不会相互接触并且它们之间有公平的间距?我还想调整图像大小以使其更小。
我在这里查看了https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html 的文档,但不确定savefig 使用哪个参数。
【问题讨论】:
-
您愿意旋转 x 轴标签吗? This 可能会有所帮助。或者,您可以在单位名称中使用“kilo”,然后简单地将它们全部除以 1000?
-
使用
plt.tick_params(axis='x', which='major', labelsize=__)
标签: python matplotlib