每个子图的旋转轴文本

时间:2013-10-09 13:11:01

标签: matplotlib

我试图绘制一个散点矩阵。我正在构建此线程Is there a function to make scatterplot matrices in matplotlib?中给出的示例。在这里,我稍微修改了代码,使轴对所有子图都可见。修改后的代码如下:

import itertools
import numpy as np
import matplotlib.pyplot as plt

def main():
    np.random.seed(1977)
    numvars, numdata = 4, 10
    data = 10 * np.random.random((numvars, numdata))
    fig = scatterplot_matrix(data, ['mpg', 'disp', 'drat', 'wt'],
            linestyle='none', marker='o', color='black', mfc='none')
    fig.suptitle('Simple Scatterplot Matrix')
    plt.show()

def scatterplot_matrix(data, names, **kwargs):
    """Plots a scatterplot matrix of subplots.  Each row of "data" is plotted
    against other rows, resulting in a nrows by nrows grid of subplots with the
    diagonal subplots labeled with "names".  Additional keyword arguments are
    passed on to matplotlib's "plot" command. Returns the matplotlib figure
    object containg the subplot grid."""
    numvars, numdata = data.shape
    fig, axes = plt.subplots(nrows=numvars, ncols=numvars, figsize=(8,8))
    fig.subplots_adjust(hspace=0.05, wspace=0.05)

    for ax in axes.flat:
        # Hide all ticks and labels
        ax.xaxis.set_visible(True)
        ax.yaxis.set_visible(True)

#        # Set up ticks only on one side for the "edge" subplots...
#        if ax.is_first_col():
#            ax.yaxis.set_ticks_position('left')
#        if ax.is_last_col():
#            ax.yaxis.set_ticks_position('right')
#        if ax.is_first_row():
#            ax.xaxis.set_ticks_position('top')
#        if ax.is_last_row():
#            ax.xaxis.set_ticks_position('bottom')

    # Plot the data.
    for i, j in zip(*np.triu_indices_from(axes, k=1)):
        for x, y in [(i,j), (j,i)]:
            axes[x,y].plot(data[x], data[y], **kwargs)

    # Label the diagonal subplots...
    for i, label in enumerate(names):
        axes[i,i].annotate(label, (0.5, 0.5), xycoords='axes fraction',
                ha='center', va='center')

    # Turn on the proper x or y axes ticks.
    for i, j in zip(range(numvars), itertools.cycle((-1, 0))):
        axes[j,i].xaxis.set_visible(True)
        axes[i,j].yaxis.set_visible(True)
    fig.tight_layout()
    plt.xticks(rotation=45)
    fig.show()
    return fig

main()

我似乎无法旋转所有子图的x轴文本。可以看出,我尝试过plt.xticks(rotation = 45)技巧。但这似乎只为最后一个子图执行旋转。

3 个答案:

答案 0 :(得分:26)

plt仅作用于当前的活动轴。你应该把它带到最后一个循环中,你可以将一些标签可见性设置为True:

# Turn on the proper x or y axes ticks.
for i, j in zip(range(numvars), itertools.cycle((-1, 0))):
    axes[j,i].xaxis.set_visible(True)
    axes[i,j].yaxis.set_visible(True)

    for tick in axes[i,j].get_xticklabels():
        tick.set_rotation(45)
    for tick in axes[j,i].get_xticklabels():
        tick.set_rotation(45)

答案 1 :(得分:23)

只需遍历与图形相关的轴,将活动轴设置为迭代对象,然后修改:

tell application "app"
    activate
end tell

tell application "System Events"
    # Get the frontmost app's *process* object.
    set frontAppProcess to first application process whose frontmost is true
end tell

# Tell the *process* to count its windows and return its front window's name.
tell frontAppProcess
    if count of windows > 0 then
       set window_name to name of front window
    end if
end tell

答案 2 :(得分:11)

for ax in fig.axes:
    ax.tick_params(labelrotation=90)