Doubtful work arises here until the real problem is solved:
Diagonal lines are just the empty space between the triangles, so what we see is a space behind the patches. Silly idea: Let’s fill this space with appropriate colors instead of white.
To do this, we copy all the objects and shift them with just a small bit.
Code:
hist(randn(1,1000)); colorbar('Location','SouthOutside'); print('test.pdf','-dpdf'); %// print original for comparison f1 = gcf; g = get(f1,'children'); n = length(g); copyobj(g,f1); %// copy all figure children
The copied objects are now the first n elements in the 2*n f1.Children . They are definitely on top of old objects.
g=get(f1,'children'); for i=1:n; if strcmpi(g(i).Type,'axes'); set(g(i),'color','none','position',g(i).Position+[0.0001 0 0 0]); set(g(i+n),'position',g(i+n).Position); %// important! end; end; print('test2.pdf','-dpdf');
Explanation:
g = get(f1,'children'); gets all axes, color panels, etc. within the current drawing.
colorbar objects are bound to axes, so we only need to move the axes children.
Setting color to none makes the background of the new axes transparent (since they are on top of the old ones).
g(i).Position+[0.0001 0 0 0] shifts the new axes by 0.0001 normalized units to the right.
set(g(i+n),'position',g(i+n).Position); This line seems unnecessary, but the last image below shows what happens when you print if you do not turn it on.
Depending on the types of graphic objects you have built, you may need to customize it to suit your needs, but this code should work if you only have colorbar and axes objects.
Original:

Using hack:

Without the string %// important! :
