在Pandas中使散点图的标签垂直和水平

Jac*_*ain 8 python pandas

我正在使用Pandas绘制散点图矩阵:from pandas.tools.plotting import scatter_matrix.问题是,列中的列名DataFrame太长,我需要它们在x轴上是垂直的,在y轴上是水平的,所以它们可以适合.我无法弄清楚如何在熊猫中做到这一点.我知道怎么做,matplotlib但不是在熊猫.

我的代码:

pylab.clf()
df = pd.DataFrame(X, columns=the_labels)
axs = scatter_matrix(df, alpha=0.2, diagonal='kde')
Run Code Online (Sandbox Code Playgroud)

编辑:我需要使用pylab.clf()因为我正在绘制很多数字,所以pylab.figure()每次调用太耗费内存.

Kir*_*n J 14

这个答案的主要帮助:https://stackoverflow.com/a/18994338/2632856

a = [[1,2], [2,3], [3,4], [4, 5], [1, 6], [2,7], [1,8]]
df = pd.DataFrame(a,columns=['askdabndksbdkl','aooweoiowiaaiwi'])
axs = pd.scatter_matrix( df, alpha=0.2, diagonal='kde')
n = len(df.columns)
for x in range(n):
    for y in range(n):
        # to get the axis of subplots
        ax = axs[x, y]
        # to make x axis name vertical  
        ax.xaxis.label.set_rotation(90)
        # to make y axis name horizontal 
        ax.yaxis.label.set_rotation(0)
        # to make sure y axis names are outside the plot area
        ax.yaxis.labelpad = 50
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


bjo*_*sen 5

scatter_matrix返回一个二维的matplotlib子图数组.这意味着您应该能够遍历两个数组并使用matplotlib函数来旋转轴.根据用于实现的源scatter_matrix和私有帮助程序函数_label_axis,看起来您应该能够执行所有绘图的旋转:

from matplotlib.artist import setp

x_rotation = 90
y_rotation = 90

for row in axs:
    for subplot in row:
        setp(subplot.get_xticklabels(), rotation=x_rotation)
        setp(subplot.get_yticklabels(), rotation=y_rotation)
Run Code Online (Sandbox Code Playgroud)

我没有一个好的方法来测试这个,所以它可能需要一些玩耍.