ich kämpfe schon seit längerem mit einem Colorbar-Problem:
Ich möchte Links eine Grafik und rechts davon die Colorbar haben. Derzeit verzerrt mir aber die Colorbar die eigentliche Grafik:
hier ist der Sourcecode dazu:
Code: Alles auswählen
'''
Created on 06.03.2013
@author:
'''
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.ticker import NullLocator
def phases(tls, maxWeight=None, ax=None):
if not ax:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
if not maxWeight:
maxWeight = 2**np.ceil(np.log(np.abs(tls).max())/np.log(2))
minarray = []
for arrays in tls:
minarray.append(min(arrays))
ax.patch.set_facecolor('white')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(NullLocator())
ax.yaxis.set_major_locator(NullLocator())
cmap = plt.get_cmap('PuBu')
print min(minarray)
y=0
for i in tls:
x=0
y+=1
for j in i:
x+=1
color = cmap(interpolateZeroOne(min(minarray), 1.0,j))
if j == 1.001: color = 'white'
size = 1 #np.sqrt(np.abs(interpolateZeroOne(min(minarray), 1.0,j)))
rect = Rectangle([x - size / 4, y - size / 4], size, size, facecolor=color, edgecolor='black')
ax.add_patch(rect)
ax.autoscale_view()
ax.set_ylim(*ax.get_ylim()[::-1])
data = tls
cax = ax.imshow(data, interpolation='nearest', cmap=plt.get_cmap('PuBu'))
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])
def interpolateZeroOne(minV, maxV, value):
return maxV/(maxV-minV)*(value-minV)
if __name__ == '__main__':
allCorrP = [[1.0, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001], [0.997, 1.0, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001], [0.959, 0.956, 1.0, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001], [0.814, 0.804, 0.919, 1.0, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001], [-0.122, -0.139, 0.124, 0.473, 1.0, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001], [0.332, 0.323, 0.546, 0.813, 0.878, 1.0, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001], [0.784, 0.767, 0.886, 0.988, 0.502, 0.821, 1.0, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001], [0.832, 0.826, 0.938, 0.996, 0.439, 0.79, 0.976, 1.0, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001], [0.782, 0.778, 0.904, 0.994, 0.506, 0.843, 0.979, 0.992, 1.0, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001], [0.821, 0.814, 0.932, 0.996, 0.459, 0.8, 0.981, 0.997, 0.992, 1.0, 1.001, 1.001, 1.001, 1.001, 1.001], [0.859, 0.85, 0.947, 0.995, 0.397, 0.762, 0.981, 0.995, 0.986, 0.995, 1.0, 1.001, 1.001, 1.001, 1.001], [0.265, 0.24, 0.471, 0.763, 0.908, 0.959, 0.799, 0.731, 0.774, 0.748, 0.71, 1.0, 1.001, 1.001, 1.001], [0.35, 0.344, 0.56, 0.823, 0.865, 0.997, 0.826, 0.801, 0.854, 0.81, 0.773, 0.949, 1.0, 1.001, 1.001], [0.785, 0.768, 0.886, 0.987, 0.497, 0.817, 0.997, 0.977, 0.977, 0.98, 0.981, 0.799, 0.823, 1.0, 1.001], [0.795, 0.788, 0.914, 0.996, 0.494, 0.83, 0.981, 0.996, 0.996, 0.996, 0.991, 0.772, 0.84, 0.981, 1.0]]
phases(allCorrP)
plt.show()
Danke schon einmal im Voraus!
ps: ich weiß nicht genau, in welche Kategorie das fällt, daher poste ich es mal unter Allgemeines