dopayi.blogg.se

Legend matlab
Legend matlab








legend matlab

In both cases, after you have set the properties, you need to all legend() without any arguments to toggle the legend on. This can be done during the plot() call or using set() on the handle. Image by author Using the “DisplayName” PropertyĪnother convenient way to add the legend labels is to set the “DisplayName property on the lines as they are plotted.

legend matlab

legStr = įor convenience, this method will be used for the rest of the examples. This is most useful when you are programmatically creating the legend string. In addition to specifying the labels as individual character strings, it is often convenient to collect the strings in a cell array. Plot( x, cos( 4x )/3, '^g', 'linewidth', 2 ) Ī legend can be added with the following command. Plot( x, sin( x ), 'rx', 'linewidth', 2 ) The basic syntax is: legend( ‘Description 1’, ‘Description 2’, … ).įor the examples in this section, we will generate a sample figure using the following code. The simplest way to use the function is to pass in a character string for each line on the plot.

legend matlab

The legend() function in MATLAB/Octave allows you to add descriptive labels to your plots.

#Legend matlab code

The source code for the included examples can be found in the GitHub repository. In this article, I cover the basic use of the legend() function, as well as some special cases that I tend to use regularly. Luckily, MATLAB/Octave include the legend() function which provide some flexible and easy-to-use options for generating legends. This dynamic-legend post covered details about attaching a listener to a child of the legend axes, in Matlab the legend function creates its own set of axes on the figure to display the legend.Īrmed with the information that legend axes objects could have listeners attached, I considered that these might be the source of redrawing the legend for printing.Plot legends are essential for properly annotating your figures. In my search for information on how the legend worked I found the undocumented Matlab article about generating dynamic legends. I couldn’t find the obvious culprit, stepping as far as I could through the print function there didn’t appear to be any call to the legend function. My initial thought was to find out where the figure got redrawn for printing or export and override the standard legend call with a call to my new function. However, as soon as the figure was printed or exported to an image file, internal Matlab workings would redraw the figure, including the legend, thereby undoing my careful legend crafting, squeezing it back into one column (Yuck!):Īs we wanted to automatically output images files, I had to delve into the hidden areas of Matlab to try to solve this problem.

legend matlab

So where is the link with undocumented Matlab?Īs mentioned in the original columnlegend entry, it was relatively simple to redraw the legend as required on the screen. Multi-column legend Delving into the Undocumented Matlab Although lacking some features, columnlegend gave me a good start on developing what I wanted for a multi column legend, culminating in gridLegend: I wanted some way of generating a more compact legend format.įortunately earlier in the year, an entry on Matlab Central allowing a multi-column legend to be generated was posted, columnlegend. Using the standard Matlab legend function resulted in a long legend over twice the size of the associated plot: We often need to plot 40 or 50 lines of test data to visualize trends or outliers, using the legend to identify the number of test hours against each test specimen. However, there is only so much data consolidation possible before you start losing the information required. In my day job we have a requirement to condense a large quantity of data onto summary charts. This tale relates the development of a legend plotting routine, gridLegend, in an effort to improve the presentation. Whilst I enjoy using the many time saving features of Matlab, one area where I feel it suffers is the technical plotting and annotation. Adrian will describe a very handy utility that shows how basic built-in Matlab functions can be improved and customized by just a bit of fiddling under Matlab’s hood. I would like to welcome guest blogger Adrian Cherry.










Legend matlab