Pandas dataframe.hist() 更改子图的标题大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46188580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:26:23  来源:igfitidea点击:

Pandas dataframe.hist() change title size on subplot?

pythonpandasmatplotlibdataframe

提问by jayko03

I am manipulating DataFrame using pandas, Python. My data is 10000(rows) X 20(columns) and I am visualizing it, like this.

我正在使用 Pandas、Python 操作 DataFrame。我的数据是 10000(行)X 20(列),我正在可视化它,就像这样。

df.hist(figsize=(150,150))

However, if I make figsize bigger, each of subplots' title, which is name of each columns, get really small or graphs overlap each other and it makes impossible to distinguish.

但是,如果我将 figsize 变大,则每个子图的标题(即每列的名称)会变得非常小或图形彼此重叠并且无法区分。

Is there any clever way to fix it?

有什么聪明的方法可以解决吗?

Thank you!

谢谢!

回答by Zero

There could be cleaner ways. Here are two ways.

可能有更清洁的方法。这里有两种方法。

1)You could set properties of subplots like

1)您可以设置子图的属性,例如

fig = df.hist(figsize=(50, 30))
[x.title.set_size(32) for x in fig.ravel()]

enter image description here

在此处输入图片说明

2)Another way, is to set matplotlib rcParamsdefault parameters

2)另一种方式,是设置matplotlib rcParams默认参数

import matplotlib

params = {'axes.titlesize':'32',
          'xtick.labelsize':'24',
          'ytick.labelsize':'24'}
matplotlib.rcParams.update(params)
df.hist(figsize=(50, 30))

enter image description here

在此处输入图片说明



Default Issue

默认问题

This is default behavior with very small labels and titles in subplots.

这是子图中非常小的标签和标题的默认行为。

matplotlib.rcParams.update(matplotlib.rcParamsDefault)  # to revert to default settings
df.hist(figsize=(50, 30))

enter image description here

在此处输入图片说明

回答by ImportanceOfBeingErnest

I would not recommend to make the figure much larger then 10 inch in each dimension. This should in any case be more than enough to host 20 subplots. And not making the figure so large will keep fontsize reasonable.
In order to prevent plot titles from overlappig, you may simply call plt.tight_layout().

我不建议使图形在每个维度上都比 10 英寸大得多。无论如何,这应该足以容纳 20 个子图。并且不要使图形如此大将保持字体大小合理。
为了防止情节标题重叠,您可以简单地调用plt.tight_layout().

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(1000,20))
df.hist(figsize=(10,9), ec="k")

plt.tight_layout()
plt.show()

enter image description here

在此处输入图片说明