Combine Multiple Plot Components into Single Plot Legend Entry

12 ビュー (過去 30 日間)
AmericanExpat26
AmericanExpat26 2017 年 12 月 14 日
編集済み: Marco 2019 年 8 月 26 日
I am trying to prepare a plot legend for a 2x2 plot figure. Each subplot has multiple plot components. Some these plot components are related; for example, I use "scatter" to plot dataset points and then "plot" to join them with a line of the same color. However, when I try to create a plot legend, I can never combine multiple plot components into a single plot legend entry, so the plot legend shows the a point with a line going through it instead of just the point itself. An example figure I am working with is attached.
A quick example of the code I use is below.
For the top left subplot:
scatter(hour2,ph2,30,'o','filled','MarkerFaceColor',[0 0 0.8]); %Entry 1
hold on
plot(hour2,ph2,'Color',[0 0 0.8],'LineWidth',1,'LineStyle','-'); %Entry 2
hold off
Then:
plotleg = legend('Entry 1','Entry 2,'Location','bestoutside','Orientation','vertical');
legend('boxon');
set(plotleg,'FontSize',11);
In that example, each will appear as its own plot legend entry. Is it possible to modify that example code in any way so that the scatter+plot components appear as a single plot legend entry or is there a better way to accomplish this altogether? Thanks in advance for the help.

採用された回答

Amy
Amy 2017 年 12 月 20 日
Instead of using both 'scatter' and 'plot', why not just use 'plot' to create a line plot with markers?
You'd change the call to 'plot' in your example as follows:
plot(hour2,ph2,'-o','Color',[0 0 0.8],'LineWidth',1,'MarkerFaceColor',[0 0 0.8]);
Then your legend will only have one entry for the plot. It sounds like this is what you are trying to accomplish.
  2 件のコメント
AmericanExpat26
AmericanExpat26 2017 年 12 月 21 日
Thanks. Just added an entry for 'MarkerEdgeColor' to complete the line.
Marco
Marco 2019 年 8 月 26 日
編集済み: Marco 2019 年 8 月 26 日
Hi Amy, I wish to combine a line plot and scatter plot into one entry in the Matlab legend. However I cannot use the suggestion mentioned, as my scatter plot features experimental data points, and my line plot features an exponential fit of the data - so they are two separate plotting functions. I would like that the symbol from the scatter plot appears over the line style of the line plot in the legend.
Is this possible? Any alternative suggestions to that described above?
Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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