# Uq\_graphics: a centralized graphics library for UQLab

**URL:** <https://uqworld.org/t/uq-graphics-a-centralized-graphics-library-for-uqlab/154>\
**Category:** Developers Corner\
**Tags:** graphics, library, plotting, visualization\
**Created:** [May 13, 2019, 10:26am UTC](https://uqworld.org/t/uq-graphics-a-centralized-graphics-library-for-uqlab/154 "2019-05-13T10:26:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![philippe](https://uqworld.org/user_avatar/uqworld.org/philippe/32/215_2.png) [@philippe](https://uqworld.org/u/philippe)\
**Post date:** [May 13, 2019, 10:26am UTC](https://uqworld.org/t/uq-graphics-a-centralized-graphics-library-for-uqlab/154/1 "2019-05-13T10:26:34Z")

</div>

In both research or industrial applications, the visualization of the results of an analysis is essential, and can greatly improve their interpretation. After all, _a picture is worth a thousand words_ and we do believe this not only applies to prose but also (and maybe even more so) to numbers and code. Thus, the display of _INPUT_, _MODEL_ and _ANALYSIS_ results has always been an important feature of UQLab.

Through the years, [UQLab has been growing steadily, and many new features were added](https://uqworld.org/t/what-is-the-history-of-uqlab), each with its own display functionality. We tried to keep the visuals as uniform as possible to maintain a _UQLab look_ in our plots. However, without some basic, central formatting functions, the ever-growing number of plots has been hard to manage. Furthermore, if users want to customize the figures produced by the _display_ function, they have to either manually format each figure after it is created, or delve deeper into the code, which can be cumbersome and time-consuming.

## `uq_graphics`: killing two birds with one stone

After the release of UQLab V1.2 we started to develop improved plotting tools and collected them in the `uq_graphics` library. This library comprises our own versions of frequently used plot functions (like `uq_plot` 📈 , `uq_bar` 📊, etc.). At the same time, it includes several underlying formatting functions that set the defaults for plot properties like `LineWidth`, `Markers` and `EdgeColor`, as well as the default format of the axes, color order, and color map. However, the user can modify the defaults by providing custom format specifications. You might have noticed that some of those functions were used in earlier code. However, until now they were p-coded, far less extensive and a bit impractical.

This new setup allows for two improvements to the UQLab framework. On the one hand, all _display_ functions or plots can be cleared of formatting, streamlining the code while maintaining a unique and consistent look and also reducing the number of entry points for future changes in the graphics. On the other hand, UQLab users can now easily modify those functions to get plots with their desired format straight from the _display_ functions. This library will be released with UQLab V1.3 by the end of Summer 2019.

## Example

To give you an example, consider the last figure produced in the Sobol’ indices example of the Sensitivity Module. Here we draw results from three different analyses and compare them in one bar plot. In V1.2 the code for the plot looks like this:

```matlab
uq_figure('Position', [50 50 500 400])
cm = colormap; % Use default MATLAB colormap
uq_bar((1:8)-0.25, mySobolResultsMC.FirstOrder, 0.25,...
    'FaceColor', cm(33,:), 'EdgeColor', 'none')
hold on
uq_bar(1:8, mySobolResultsPCE.FirstOrder, 0.25,...
    'FaceColor', cm(1,:), 'EdgeColor', 'none')
uq_bar((1:8)+0.25, mySobolResultsLRA.FirstOrder, 0.25,...
    'FaceColor', cm(64,:), 'EdgeColor', 'none')
% Set axes limits
xlim([0 9])
ylim([0 1])
% Set labels
uq_setInterpreters(gca)
xlabel('Variable name')
ylabel('First-order Sobol'' indices')
set(gca, 'XTick', 1:length(InputOpts.Marginals),...
    'XTickLabel', mySobolResultsPCE.VariableNames, 'FontSize', 14)
% Set legend
uq_legend({...
    sprintf('MC-based (%.0e simulations)', mySobolResultsMC.Cost),...
    sprintf('PCE-based (%d simulations)', myPCE.ExpDesign.NSamples),...
    sprintf('LRA-based (%d simulations)', myLRA.ExpDesign.NSamples)},...
    'Location', 'northeast', 'Interpreter', 'latex', 'FontSize', 14)

```

Making use of the `uq_graphics` library we can cut out all the formatting getting a much slimmer code that just deals with the data and text:

```matlab
% Create a bar plot to compare the first-order Sobol' indices:
uq_figure('FileName', 'FirstOrderIndices.fig')
uq_bar((1:8)-0.25, mySobolResultsMC.FirstOrder, 0.25)
hold on
uq_bar(1:8, mySobolResultsPCE.FirstOrder, 0.25)
uq_bar((1:8)+0.25, mySobolResultsLRA.FirstOrder, 0.25)
% Set axes limits
xlim([0 9])
ylim([0 1])
% Set labels
xlabel('Variable name')
ylabel('First-order Sobol'' indices')
set(gca, 'XTick', 1:length(InputOpts.Marginals),...
    'XTickLabel', mySobolResultsPCE.VariableNames)
% Set legend
uq_legend({...
    sprintf('MC-based (%.0e simulations)', mySobolResultsMC.Cost),...
    sprintf('PCE-based (%d simulations)', myPCE.ExpDesign.NSamples),...
    sprintf('LRA-based (%d simulations)', myLRA.ExpDesign.NSamples)},...
    'Location', 'northeast')

```

Without any manual formatting, this results in the figure below with our desired UQLab format:  
 ![FirstOrderIndices](https://uqworld.org/uploads/default/original/1X/7e01ce1295ccaeda1fb7bfbd48c775d5f5701371.png)

## Improvements in later updates

Since UQLab aims to be used by practitioners as well as researchers, easy access to its settings is important, and setting the graphics defaults in structures in m-files might not be optimal. Therefore, in future versions we will provide custom initialization files, where settings can be made in text form which can then be loaded by UQLab upon startup.

As you might know, _INPUT_, _MODEL_ and _ANALYSIS_ objects are stored in a UQLab session upon creation by the core. In the future we will consider extending `UQLabCore` to also handle all figures created through `uq_graphics`, making it easier to keep track of all produced plots during a session.

So now you know a little more about our plans with the graphics in UQLab. We constantly try to improve the software for our users, so if you have any feedback or ideas for visualizations, please let us know!
