How do I make a legend for the marker size in a bubble plot?

1 回表示 (過去 30 日間)
Andrew Robison
Andrew Robison 2017 年 5 月 8 日
回答済み: Walter Roberson 2017 年 5 月 8 日
I'm trying to make a plot to show the value of four functions for a given point. I'd like to uses a scatter plot in the x and y, and then size and color of the bubbles for the values of the other two functions. I've been able to do that, but how can I make a legend for the bubble size? Also, the bubbles are very small (almost invisible) if the values are small. Is there any way to scale the bubbles to a larger size? Here's an example of the code I'm running to make the plot.
clear
clc
x=abs(randn(1,10));
y=abs(randn(1,10));
s=80*abs(randn(1,10));
c=abs(randn(1,10));
figure()
scatter(x,y,s,c,'filled')
colormap jet
colorbar
ylabel(colorbar,'color','FontSize',12)
xlabel('x')
ylabel('y')

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 5 月 8 日
Each scatter() plot generates exactly one graphics object that can have its own legend entry.
plot() can generate multiple graphics objects, for two reasons:
  1. you can pass multiple x/y pairs to plot, like plot(x1, y1, x2, y2, ...)
  2. if y is an array with multiple columns (after adjusting its orientation to match x), then one graphics object will be created for each column of y
Each graphics object created by plot() can have a separate legend.
At present, you have indicate you have four functions but only one graphics object. That only gets you room for one legend entry.
So... what you need to do is fake it. Create dummy graphics objects whose x y coordinates are nan or inf (because graphics will not render those), and with whatever graphics attributes you want, and pass those as the first parameter to legend(), along with the desired strings.
For example if you had three different bubble colors, you might code three dummy graphics objects (one in each color) so that you could have strings describing the meaning of each.

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by