[ad_1]
Introduction
In knowledge evaluation, a well-designed graph may help make clear your insights however a poorly annotated one can confuse and distract your viewers. That’s why correct annotation, together with legends, is crucial to creating efficient graphs.
Legends play an important function in making graphs extra readable by distinguishing between totally different teams, classes, or knowledge sequence. A well-placed legend helps be sure that your message comes throughout clearly.
On this weblog, we’ll stroll by the best way to add and customise legends in GAUSS graphics, protecting:
Robotically Including Legends with the by Key phrase
When utilizing a system string with the by key phrase, GAUSS mechanically generates a legend primarily based on the categorical variable.
For instance, let’s create a scatter plot utilizing the built-in crabs.dta dataset:
// Load knowledge
fname = getGAUSSHome("examples/crabs.dta");
crabs = loadd(fname);
// Create scatter plot with computerized legend
plotScatter(crabs, "rear_width ~ body_depth + by(intercourse)");When the by key phrase is used with the specific variable, intercourse GAUSS:
- Plots a separate shade for every group.
- Robotically creates a legend indicating totally different teams.
- Features a title on the legend.
These legends are helpful once we simply want a fast look at our knowledge. Nonetheless, they do not permit for customized formatting. To make use of customized formatting we have to use a plotControl construction.
Setting Up a plotControl Construction
To customise a GAUSS plot, step one is to declare and initialize a plotControl construction. This construction is used for all plot-related settings, together with axis labels, colours, fonts, legends, and extra.
Why Use a plotControl Construction?
The plotControl construction offers a versatile and arranged strategy to modify a plot’s look. As a substitute of manually formatting the plot after it’s created, we will programmatically set all customizations upfront. This protects us effort and time when we have to reproduce our graphs.
To make use of this construction we:
- Declare a
plotControlconstruction. - Fill it with default settings utilizing plotGetDefaults.
- Modify the construction’s properties as wanted.
- Cross the construction when calling our GAUSS plotting perform.
Declaring and Initializing the plotControl Construction
Each plot customization begins with the next setup:
// Declare plot management construction
struct plotControl myPlot;
// Fill with default settings for an XY plot
myPlot = plotGetDefaults("xy");Discover that the defaults are particular to the plot sort we’re making. For instance, if we have been making a bar or scatter plot, we might use “bar” or “scatter” as a substitute.
As soon as the plotControl construction is initialized, we will customise all graph properties—resembling including a legend.
Including a Fundamental Legend
After declaring and initializing our plotControl construction, we will use the plotSetLegend perform so as to add a default styled legend to any plot.
The perform takes two required inputs:
- A pointer to a plot management construction.
- A string array containing legend labels.
Moreover, two non-compulsory arguments could also be used:
- A string specifying the legend location.
- A scalar indicating vertical or horizontal orientation.
Including a Default Legend
Let us take a look at including a easy legend to an XY plot with the default location and orientation:
// Declare plot management construction
struct plotControl myPlot;
// Fill with default settings for xy plot
myPlot = plotGetDefaults("xy");
// Pattern knowledge
x = seqa(1, 1, 10);
y1 = x + rndn(10, 1);
y2 = x - 0.5 + rndn(10, 1);
// Specify legend labels
// utilizing '$|" to concatenate
// particular person labels
label = "Group 1"$|"Group 2";
// Arrange primary legend
plotSetLegend(&myPlot, label);
// Create XY plot
plotXY(myPlot, x, y1~y2);Altering the Legend Location
By default our legend is within the high, proper nook of our plot canvas. This may increasingly not at all times be the best location, as we will see within the plot above.
Happily, the location enter permits us to specify a distinct location. The location enter can both be the xy coordinates for the highest left of the legend, or a string. Setting xy coordinates permits for exact placement, however can typically be extra cumbersome.
When specifying the legend location utilizing a string, you might use a number of of the next:
- Vertical location:
"high"(default),"vcenter", or"backside". - Horizontal location:
"left","hcenter", or"proper"(default). - Inside/outdoors location:
"inside"(default) or"outdoors"
For instance, let’s change the legend location to the underside, proper nook of the plot:
// Specify legend labels
// utilizing '$|" to concatenate
// particular person labels
label = "Group 1"$|"Group 2";
// Place in backside proper nook
location = "backside proper";
// Set legend
plotSetLegend(&myPlot, label, location);
// Create XY plot
plotXY(myPlot, x, y1~y2);These location parts could be laid out in any order. For instance, we might get the identical outcomes specifying the placement like this:
// Place in backside proper nook
location = "proper backside";We might create a really comparable graph by specifying the highest left of the legend to be at x=7.5 and y=2 like this:
// Specify xy coordinates for the highest left nook of the legend.
location = { 7.5, 2 };Altering the Legend Orientation
The plotSetLegend process additionally permits us to specify if the sequence are listed horizontally or vertically utilizing the non-compulsory orientation enter.
The orientation enter is ready to:
- 1 for a vertical sequence record (default).
- 0 for a horizontal sequence record.
// Specify legend labels
// utilizing '$|" to concatenate
// particular person labels
label = "Group 1"$|"Group 2";
// Place in backside proper nook
location = "backside proper";
// Set to horizontal record
orientation = 0;
// Set legend
plotSetLegend(&myPlot, label, location, orientation);
// Create XY plot
plotXY(myPlot, x, y1~y2);Superior Legend Formatting
Along with the fundamental legend, GAUSS offers a number of features to customise legend look.
GAUSS Legend Customization Capabilities | ||
|---|---|---|
| Perform Identify | Description | Instance |
| plotSetLegend | Defines a legend for the plot with customized labels. | plotSetLegend(&myPlot, label [, location, orientation]); |
| plotSetLegendBkd | Units the opacity and shade for the background of a graph legend. | plotSetLegendBkd(&myPlot, opacity [, bkd_clr]); |
| plotSetLegendBorder | Controls the colour and thickness of the legend border. | plotSetLegendBorder(&myPlot, clr [, thickness]); |
| plotSetLegendFont | Customizes the font fashion, dimension, and shade of legend textual content. | plotSetLegendFont(&myPlot, font [, font_size, font_color]); |
| plotSetLegendTitle | Controls the legend title. | plotSetLegendTitle(&myPlot, title) |
| plotSetTextInterpreter | Controls the textual content interpreter settings for a graph. | plotSetTextInterpreter(&myPlot, interpreter [, location]); |
Instance: Superior Legend Formatting
Let us take a look at one other plotting instance and discover a few of the superior legend formatting choices.
To get began, we are going to simulate some knowledge:
/*
** Create the sequence 0.25, 0.5, 0.75...3
*/
x = seqa(0.25, 0.25, 12);
y = sin(x);and setup our plotControl construction
// Declare plotControl construction
// and fill with default settings for XY plots
struct plotControl myPlot;
myPlot = plotGetDefaults("xy");We wish the legend for this plot to:
- Be horizontally centered and positioned outdoors the underside of the plot.
- Use 14 pt., “darkish blue”, Arial font.
- Have a “mild grey” border with a thickness of two pixels.
- Render and interpret labels utilizing latex.
Labels and Location
We set the labels and placement utilizing the plotSetLegend process:
/*
** Fundamental legend settings
*/
// Set label
label = "sin{x}";
// Set location
location = "backside hcenter outdoors";
// Set legend
plotSetLegend(&myPlot, label, location);Legend Font Properties
The plotSetLegendFont perform permits us to regulate the font fashion, dimension, and shade of the legend textual content.
/*
** Legend font
*/
// Set font
font_style = "Arial";
// Set font dimension
font_size = 14;
// Set font shade
font_clr = "darkish blue";
// Set all legend font properties
plotSetLegendFont(&myPlot, font_style, font_size, font_clr);Customizing The Legend Border
The plotSetLegendBorder process units the colour and width of the border.
/*
** Legend border
*/
// Set border shade
border_clr = "mild grey";
// Border width
border_width = 2;
// Set the legend border
plotSetLegendBorder(&myPlot, border_clr, border_width);Altering Textual content Interpretation
By default, GAUSS treats legend textual content as plain textual content. Nonetheless, we will allow LaTeX-style formatting utilizing plotSetTextInterpreter:
/*
** Set textual content interpret to interpret
** latex for legend labels
*/
plotSetTextInterpreter(&myPlot, "latex", "legend");Producing Our Plot
// Create XY plot
plotXY(myPlot, x, y);Conclusion
On this weblog, we lined other ways to customise legends in GAUSS plots:
- Including a legend utilizing
plotSetLegend. - Modifying fonts, backgrounds, and borders for higher visualization.
- Using LaTeX formatting and including legend titles.
- Robotically producing legends utilizing the
bykey phrase.
These strategies improve the readability of your visualizations, making it simpler to interpret outcomes.
Additional Studying
- Find out how to combine, match and magnificence totally different graph sorts
- Find out how to Interactively Create Reusable Graphics Profiles
- Find out how to Create Tiled Graphs in GAUSS
- Visualizing COVID-19 Panel Knowledge With GAUSS 22
- Superior Formatting Methods for Creating AER High quality Plots
- Introduction to Environment friendly Creation of Detailed Plots
Eric has been working to construct, distribute, and strengthen the GAUSS universe since 2012. He’s an economist expert in knowledge evaluation and software program growth. He has earned a B.A. and MSc in economics and engineering and has over 18 years of mixed business and educational expertise in knowledge evaluation and analysis.
[ad_2]

































Mengdie (Flora) Wang is a Information Scientist at AWS Generative AI Innovation Middle, the place she works with clients to architect and implement scalable Generative AI options that handle their distinctive enterprise challenges. She focuses on mannequin customization methods and agent-based AI techniques, serving to organizations harness the total potential of generative AI expertise. Previous to AWS, Flora earned her Grasp’s diploma in Laptop Science from the College of Minnesota, the place she developed her experience in machine studying and synthetic intelligence.
Jason Zhang has experience in machine studying, reinforcement studying, and generative AI. He earned his Ph.D. in Mechanical Engineering in 2014, the place his analysis targeted on making use of reinforcement studying to real-time optimum management issues. He started his profession at Tesla, making use of machine studying to automobile diagnostics, then superior NLP analysis at Apple and Amazon Alexa. At AWS, he labored as a Senior Information Scientist on generative AI options for purchasers.
Rachel Hanspal is a Deep Studying Architect at AWS Generative AI Innovation Middle, specializing in end-to-end GenAI options with a give attention to frontend structure and LLM integration. She excels in translating advanced enterprise necessities into progressive functions, leveraging experience in pure language processing, automated visualization, and safe cloud architectures.
Zubair Nabi is the CTO and Co-Founding father of Kscope, an Built-in Safety Posture Administration (ISPM) platform. His experience lies on the intersection of Huge Information, Machine Studying, and Distributed Programs, with over a decade of expertise constructing software program, information, and AI platforms. Zubair can also be an adjunct school member at George Washington College and the writer of Professional Spark Streaming: The Zen of Actual-Time Analytics Utilizing Apache Spark. He holds an MPhil from the College of Cambridge.
Suparna Pal – CEO & Co-Founding father of kscope.ai – 20+ years of journey of constructing progressive platforms & options for Industrial, Well being Care and IT operations at PTC, GE, and Cisco.
Wan Chen is an Utilized Science Supervisor at AWS Generative AI Innovation Middle. As a ML/AI veteran in tech trade, she has wide selection of experience on conventional machine studying, recommender system, deep studying and Generative AI. She is a stronger believer of Superintelligence and may be very passionate to push the boundary of AI analysis and software to reinforce human life and drive enterprise development. She holds Ph.D in Utilized Arithmetic from College of British Columbia and had labored as postdoctoral fellow in Oxford College.
Mu Li is a Principal Options Architect with AWS Vitality. He’s additionally the Worldwide Tech Chief for the AWS Vitality & Utilities Technical Subject Group (TFC), a neighborhood of 300+ trade and technical specialists. Li is obsessed with working with clients to realize enterprise outcomes utilizing expertise. Li has labored with clients emigrate all-in to AWS from on-prem and Azure, launch the Manufacturing Monitoring and Surveillance trade answer, deploy ION/OpenLink Endur on AWS, and implement AWS-based IoT and machine studying workloads. Outdoors of labor, Li enjoys spending time together with his household, investing, following Houston sports activities groups, and catching up on enterprise and expertise.