Reputation: 2749
I used to plot my correlation matrix like:
corr_matrix = np.corrcoef(W)
plt.matshow(corr_matrix)
labels = ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10', 'P11', 'P12', 'P13', 'P14', 'P15', ]
plt.xticks(range(0,15),labels)
plt.yticks(range(0,15),labels)
plt.colorbar()
plt.show()
Now I'm working with some provided code which is plotting different things in 2 figures over time using the axes.draw()
method.
The plotting of the figures is done with:
fig_1, axes_1 = plt.subplots(yl,5) # fig 1
fig_2, axes_2 = plt.subplots(5,1) # fig 2
and at some point after setting the data plt.draw()
is used to redraw the images.
I've tried to add another figure like:
fig_cor, axes_cor = plt.subplots(1,1)
and use
axes_cor.imshow(corr_matrix)
labels = ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10', 'P11', 'P12', 'P13', 'P14',
'P15']
axes_cor.set_xticks(range(0, 15), labels)
axes_cor.set_yticks(range(0, 15), labels)
# axes_cor.colorbar()
plt.draw()
The image is displayed, but my labels are not and when I'm trying to draw the colorbar too I get 'AxesSubplot' object has no attribute 'colorbar'
. Can anyone help me solve this?
What I want to display:
What I get:
Upvotes: 1
Views: 2889
Reputation: 4199
I think in imshow the ticks are created that dont match the label elements. May be there is an elegant solution, but the example below also achieves it.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
corr_matrix= np.random.rand(20,20)
fig_cor, axes_cor = plt.subplots(1,1)
fig_cor.set_size_inches(6, 6)
labels = ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10', 'P11', 'P12', 'P13', 'P14','P15']
myimage = axes_cor.imshow(corr_matrix)
plt.colorbar(myimage)
axes_cor.set_xticks(np.arange(0,corr_matrix.shape[0], corr_matrix.shape[0]*1.0/len(labels)))
axes_cor.set_yticks(np.arange(0,corr_matrix.shape[1], corr_matrix.shape[1]*1.0/len(labels)))
axes_cor.set_xticklabels(labels)
axes_cor.set_yticklabels(labels)
plt.draw()
results in
Upvotes: 1