Wednesday, November 22, 2017

Published 8:33 PM by with 8 comments

How Do You Make A Plot With Custom Background Colors, Two Y Axes, Custom Opacity, Custom Gridlines, And More In MATLAB?

I had an idea for a plot recently, and it took a bit of tinkering to get it to look right in MATLAB so I figured I'd post a tutorial.

The plot pictured above is taken from this post. There's a lot of customization there, and the specific things I'll address are:
  • How did I get a custom background color for a MATLAB plot?
  • How did I get the plots to have different opacities in MATLAB?
  • How did I get two, custom, y axes in MATLAB?
  • How could I get custom grid lines in MATLAB?
There's some other stuff in that and you can see the code here to get a full idea. Also...note that I'm using MATLAB 2015 and much of this has probably changed in newer versions (e.g., they added yyaxis).

Custom background color in MATLAB

There are actually two background colors in the plot: overall background, and background of the portion within the axes. For the overall background, you set a background 'Color' for the figure using something like the following:

fig = figure(1)
set(fig, 'Color', [0.24 0.21 0.28])

For the background color within the axes, you set the 'Color' of the axes. Note that since I'm setting the background to near black, the axes themselves should not be black:

set(gca, 'XColor', [0.95 0.95 0.95], 'YColor', [0.95 0.95 0.95], 'Color', [0.24 0.21 0.28])

That sets the x-axis color ('XColor'), y-axis color ('YColor'), and background color ('Color').

Custom plot opacity in MATLAB

To set the opacity of the plot itself, you set the color of the line object from the plot:

h = plot(squeeze(squeeze(decadeTemp(i, j, :))));
h.Color = [.95, .95, .95, 0.01];

That's it. The 4th element of the color is the opacity.

Custom y axes in MATLAB

Again...note that I'm using MATLAB 2015 here. For this, I created a dummy plot outside the plot limits using the plotyy function:

[ax, h1, h2] = plotyy(1:17, -100 + zeros(1, 17), 1:17, -100 + zeros(1, 17));

Then, I customized the axes. In this case, I wanted the left to be Celsius (actual value) and the right to be Fahrenheit, so I need custom labels for the right. This is ax(2), so I just set the properties I want for ax(2) to control the right y axis. An example is:

set(ax(2), 'YLim', [-3 3], 'YTick', [-3, -2, -1, 0, 1, 2, 3], 'YTickLabel', {'-5.4', '-3.6', '-1.8', '0', '1.8', '3.6', '5.4'})

You do the same for the left axis using ax(1).

Finally, I wanted to rotate the right axis label, so I used:

ylabel(ax(2), 'Temperature Anomaly (F)', 'rot', -90, 'Position', [19.5, 0, 0])

The 'Position' setting there sets it where 19.5 would occur on the x-axis.

Custom grid lines in MATLAB

The plot above has the grid lines turned off. However, I toyed with including them, and ran into an issue where setting plot opacity also affected the gridlines. Further, maybe you want the one at y = 0 to be thicker and highlighted?

The way I settled on doing this is really obvious, and it's literally to plot the grid lines as lines:

for i = 2:2:17
    plot([i i], [-2 2], 'Color', [0.95 0.95 0.95 0.5])
for i = -2:0.5:2
    plot(1:17, i + zeros(1, 16), 'Color', [0.95 0.95 0.95 1])

That plots vertical lines between y = 2 and y = -2 for x = 2, 4, 6..., makes them nearly white, and sets the opacity to 0.5. It plots horizontal lines between x = 1 and x = 17 for y = -2, -1.5, -1...

That's it. I liked this plot and hopefully this helps if someone runs into the same thing as it required me to customize the plot, generate code, then reverse engineer to fine tune it.



  1. Background removal services of product images. This service helps E-commerce websites. Good looking product images improve sales… hire background removal service

  2. You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...

  3. Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!

  4. You have a real ability for writing unique content. I like how you think and the way you represent your views in this article. I agree with your way of thinking. Thank you for sharing. Digital Marketing Sydney

  5. Wow i can say that this is another great article as expected of this blog.Bookmarked this site.. d&d dice sets

  6. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. dungeons and dragons dice

  7. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! sneakers ash scarpe

  8. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. company formation in dubai