Wednesday, November 22, 2017

Published November 22, 2017 by with 1 comment

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])
end
for i = -2:0.5:2
    plot(1:17, i + zeros(1, 16), 'Color', [0.95 0.95 0.95 1])
end

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.

      edit

1 comment:

  1. 💎 IPRO356💎
    📢 คาสิโนอันดับ 1 เล่นง่ายได้ไว 📢
    🔥 มาลุ้นโบนัสก่อนโตได้แล้วที่นี่ที่เดียว
    🎮สล็อต บาคาร่า ยิงปลา ไพ่ (ace card) มีจบครบที่เดียว
    ⚽️ คอฟุตบอล ต้องที่ IPRO356 เท่านั้น เคลียร์บิลรวดเร็วทันใจ จ่ายเลยทันที
    💎 สมัครสมาชิก ฟรี ไม่มีค่าบริการ
    📱รองรับระบบมือถือทุกรุ่นทั้ง android และ ios
    ⚖️ ฝาก-ถอน ไม่มีขั้นต่ำ ไม่ต้องทำเทิร์นเลยนะคะ
    💸 มีสูตรแจกให้ฟรีพร้อมกลุ่มนำเล่น

    ReplyDelete