熊猫图中的标签大小(scatter_matrix)

时间:2015-08-20 14:45:33

标签: python pandas

如何在熊猫情节中设置标签尺寸?

在正常情节中我做plt.xlabel('a', size=20)

In [76]: from pandas.tools.plotting import scatter_matrix

In [77]: df = DataFrame(randn(1000, 4), columns=['a', 'b', 'c', 'd'])

In [78]: scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')
Out[78]: 
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x9f39e44c>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x9f39842c>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x9f383fcc>,
        <matplotlib.axes._subplots.AxesSubplot object at 0xa58039cc>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0xa57f19ec>,
        <matplotlib.axes._subplots.AxesSubplot object at 0xa578e66c>,
        <matplotlib.axes._subplots.AxesSubplot object at 0xa5adb28c>,
        <matplotlib.axes._subplots.AxesSubplot object at 0xa5a9deec>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0xa5d67fec>,
        <matplotlib.axes._subplots.AxesSubplot object at 0xa5dc764c>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x9fdb354c>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x9e63756c>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0xa5d9ccac>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x9f6d1ecc>,
        <matplotlib.axes._subplots.AxesSubplot object at 0xa5d6f02c>,
        <matplotlib.axes._subplots.AxesSubplot object at 0xa5848e0c>]], dtype=object)

2 个答案:

答案 0 :(得分:9)

scatter_matrix()的返回是一个轴的数量,因此,没有简单的方法可以在一次传递中设置字体大小(除非使用plt.rcParam覆盖它,例如plt.rcParams['axes.labelsize'] = 20用于更改标签大小),并且必须逐个设置,例如:plt.setp(axes[0,0].yaxis.get_majorticklabels(), 'size', 15)

enter image description here

要更改所有刻度标签,假设为Axes=scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')

import pandas as pd
import matplotlib.pyplot as plt
from numpy.random import randn
from pandas.tools.plotting import scatter_matrix

df = pd.DataFrame(randn(1000, 4), columns=['a', 'b', 'c', 'd'])

Axes = scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')

#y ticklabels
[plt.setp(item.yaxis.get_majorticklabels(), 'size', 15) for item in Axes.ravel()]
#x ticklabels
[plt.setp(item.xaxis.get_majorticklabels(), 'size', 5) for item in Axes.ravel()]
#y labels
[plt.setp(item.yaxis.get_label(), 'size', 20) for item in Axes.ravel()]
#x labels
[plt.setp(item.xaxis.get_label(), 'size', 3) for item in Axes.ravel()]

enter image description here

答案 1 :(得分:2)

pandas.tools.plotting 在新版本中已弃用。

如果您尝试:

from pandas.tools.plotting import scatter_matrix

您收到以下错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-9-d908f18459b1> in <module>
----> 1 from pandas.tools.plotting import scatter_matrix

ModuleNotFoundError: No module named 'pandas.tools'

这是@ct-zhu 答案的修改/更新版本:

import pandas as pd
import matplotlib.pyplot as plt
from   numpy.random import randn

df = pd.DataFrame(randn(1000, 4), columns=['a', 'b', 'c', 'd'])

Axes = pd.plotting.scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')

#y ticklabels
[plt.setp(item.yaxis.get_majorticklabels(), 'size', 15) for item in Axes.ravel()]
#x ticklabels
[plt.setp(item.xaxis.get_majorticklabels(), 'size', 5) for item in Axes.ravel()]
#y labels
[plt.setp(item.yaxis.get_label(), 'size', 20) for item in Axes.ravel()]
#x labels
[plt.setp(item.xaxis.get_label(), 'size', 3) for item in Axes.ravel()];

Image: Scatter Matrix with Label Font Sizes modified