2016-05-24 9 views
3

Ich möchte etwas Transparenz in meinem Diagramm setzen, was ich mit alpha machen kann. Das funktioniert super, aber ich möchte auch die Farbleiste anpassen. Hier ein Beispiel:Alpha von Farbbalken in MATLAB R2015b setzen

subplot(2,1,1) 
A = imagesc(meshgrid(0:10,0:5)); 
alpha(A,1) 
colorbar 

subplot(2,1,2) 
B = imagesc(meshgrid(0:10,0:5)); 
alpha(B,.1) 
colorbar 

Das Beispiel von here genommen wird. Auf dieser Seite gibt es zwei Lösungen, aber keine funktioniert für Matlab R2015b.

enter image description here

+0

Haben Sie versucht, 'hb = colorbar; Alpha (hb, .1); '? –

+0

@MadPhysicist Ja, das funktioniert nicht. – machinery

Antwort

4

Mit HG2 Grafiken (R2014b +) können Sie einige der Menschen ohne Papiere zugrunde liegenden Handlung Objekte erhalten und die Transparenz verändern.

c = colorbar(); 

% Manually flush the event queue and force MATLAB to render the colorbar 
% necessary on some versions 
drawnow 

alphaVal = 0.1; 

% Get the color data of the object that correponds to the colorbar 
cdata = c.Face.Texture.CData; 

% Change the 4th channel (alpha channel) to 10% of it's initial value (255) 
cdata(end,:) = uint8(alphaVal * cdata(end,:)); 

% Ensure that the display respects the alpha channel 
c.Face.Texture.ColorType = 'truecoloralpha'; 

% Update the color data with the new transparency information 
c.Face.Texture.CData = cdata; 

enter image description here

Du musst vorsichtig tun, dies sein, da die colorbar kontinuierlich aktualisiert werden und diese Änderungen werden nicht bleiben. Um sie zu erhalten bleiben, während ich die Figur gedruckt, Ich habe gerade den ColorBinding Modus des Face auf etwas anderes als interpolated

c.Face.ColorBinding = 'discrete'; 

Das bedeutet, dass es nicht aktualisiert werden, wenn Sie die Farbgrenzen oder die colormap ändern. Wenn Sie eines dieser Dinge ändern möchten, müssen Sie die ColorBinding auf intepolated zurücksetzen und dann den obigen Code erneut ausführen.

c.Face.ColorBinding = 'interpolated'; 

Zum Beispiel folgende würde ein Bild mit einem transparenten colorbar für zwei Farbtabellen speichern:

c = colorbar(); 

drawnow; 

alphaVal = 0.1; 

% Make the colorbar transparent 
cdata = c.Face.Texture.CData; 
cdata(end,:) = uint8(alphaVal * cdata(end,:)); 
c.Face.Texture.ColorType = 'truecoloralpha'; 
c.Face.Texture.CData = cdata; 

drawnow 

% Make sure that the renderer doesn't revert your changes 
c.Face.ColorBinding = 'discrete'; 

% Print your figure 
print(gcf, 'Parula.png', '-dpng', '-r300'); 

% Now change the ColorBinding back 
c.Face.ColorBinding = 'interpolated'; 

% Update the colormap to something new 
colormap(jet); 

drawnow 

% Set the alpha values again 
cdata = c.Face.Texture.CData; 
cdata(end,:) = uint8(alphaVal * cdata(end,:)); 
c.Face.Texture.CData = cdata; 

drawnow 

% Make sure that the renderer doesn't revert your changes 
c.Face.ColorBinding = 'discrete'; 

print(gcf, 'Ugly_colormap.png', '-dpng', '-r300'); 
+0

Irgendwie funktioniert das nicht für mich. Ich bekomme den Fehler "Tiefgestellte Indizes müssen entweder echte positive ganze Zahlen oder logische sein." weil Größe (cdata) = [0 0]. – machinery

+0

@machinery Hmmm seltsam. Welches OS? – Suever

+0

Ich habe Windows 10, 64-Bit. – machinery