Vertical and horizontal scatter plot labels in Pandas

I use Pandas to draw a scatter matrix matrix: from pandas.tools.plotting import scatter_matrix . The problem is that the column names in the DataFrame too large, and I need them to be vertical along the x axis and horizontal along the y axis so that they can fit. I generally cannot figure out how to do this in Pandas. I know how to do this in matplotlib , but not in Pandas.

My code is:

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

Edit: I need to use pylab.clf() because I draw a lot of numbers, so calling pylab.figure() every time has too much memory.

+5
source share
2 answers

The main help with this answer is: fooobar.com/questions/1143232 / ...

 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 

enter image description here

+7
source

scatter_matrix returns a two-dimensional array of matplotlib . This means that you should be able to iterate over two arrays and use the matplotlib functions to rotate the axes. Based on the source used to implement scatter_matrix and the scatter_matrix private helper function, it looks like you can perform your rotations for all graphs with:

 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) 

I have no good way to test this so that he can play a little.

+4
source

Source: https://habr.com/ru/post/1207041/


All Articles