File Exchange

image thumbnail

plotyn(varargin)

version 1.4.0.0 (7.48 KB) by Jakub Nedbal
Plots graphs with arbitrary number of Y-axes. Generalization of plotyy.

5 Downloads

Updated 17 Apr 2015

View License

plotyn Extends plot to create a separate Y-axis for each dataset. It is a generalization of Matlab's inbuilt function PLOTYY.
Creation mode, needs to be run first:
Syntax: [hax, hlines, data] = plot4y(data)
Syntax: [hax, hlines, data] = plot4y(data, pos)
Syntax: [hax, hlines, data] = plot4y(data, pos, parent)

Update mode, to be performed on existing axes to update their plots, colors or Y-axis labels:
Syntax: plot4y(hax, hlines, data)

Inputs:
data is a struct with at least two fields (X, Y). The dimensionality of data determines the number of graphs:
* data(1).X X coordinates of first dataset (Same for all)
* data(i).Y Y coordinates of i-th dataset
* data(i).Color Color of i-th dataset (Optional)
* data(i).YLabel Y-axis label of i-th dataset (Optional)

* pos Array of 5 numbers positioning the axes on the current figure (handle) in 'Pixel' units
* parent Handle onto which the axes are drawn

Outputs:
* hax Double array containing the axes' handles
* hlines Double array containing the lines' handles
* data Struct with the input data

Example:
x = 0 : 10; data(1).X = x;
data(1).Y = x .^ 1; data(1).YLabel = 'First Y-axis';
data(2).Y = x .^ 2; data(2).YLabel = 'Second Y-axis';
data(3).Y = x .^ 3; data(3).YLabel = 'Third Y-axis';
data(4).Y = x .^ 4; data(4).YLabel = 'Fourth Y-axis';
figure;
[hax, hlines, data] = plotyn(data);
legend(hlines, 'y = x', 'y = x^2', 'y = x^3', 'y = x^4', 2, 'Location', 'NorthWest');

Now, the data in the graph can be changed for example in datasets 2 and 3:

data(2).Y = x .^ (1 / 2);
data(3).Y = x .^ (1 / 3);
plotyn(hax, hlines, data)

See also plot, plotyy

Based on:
plotyyy.m by Denis Gilbert, Ph.D.
ploty4.m by Peter (PB) Bodin

Created by modification of the aforementioned functions by:

Jakub Nedbal
April 2015
Distributed under BSD license.

Cite As

Jakub Nedbal (2020). plotyn(varargin) (https://www.mathworks.com/matlabcentral/fileexchange/50542-plotyn-varargin), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)

Yang Yang

Great Add-Ons! :+1

I am having trouble getting this routine to work.

I copied the example directly from the top of the routine. After poltyn sets the figure handle (ax), it then modifies ax to be a structure (ax.YaxisLocation = ...). That step gives me a warning.

But, then it tries on line 180 to assign ax to hax, and I get the error:

"Conversion to double from struct is not possible."

and the routine crashes.

Any ideas why this is happening?

Jakub Nedbal

Hello Leow Cheah Wei,

That could be a useful addition. Wish to give it a go? I'd be happy to upload it and credit you as a co-author.

perhaps can consider provide ability to plot to a given axes handle instead of creating a new one

Jakub Nedbal

In response to Bailey:
The code follows that I forgot to attach to my previous post follows:

figure(1)
ax1 = subplot(2,1,1); set(ax1, 'Units', 'pixels'); % create subplot, change units to pixels
pos11 = get(ax1, 'Position'); % store subplot position
ax2 = subplot(2,1,2); set(ax2, 'Units', 'pixels'); % create subplot, change units to pixels
pos12 = get(ax2, 'Position'); % store subplot position
delete([ax1, ax2]); % delete subplots

data(1) = struct('X', 1:100, 'Y', randn(1,100), 'YLabel', 'label1');
data(2) = struct('X', 1:100, 'Y', randn(1,100), 'YLabel', 'label2');
hax1 = plotyn(data);
set(hax1, 'Position', pos11); % Set positions to the subplots
data(1) = struct('X', 1:100, 'Y', randn(1,100), 'YLabel', 'label3');
data(2) = struct('X', 1:100, 'Y', randn(1,100), 'YLabel', 'label4');
hax2 = plotyn(data);
set(hax2, 'Position', pos12); % Set positions to the subplots

Jakub Nedbal

In response to Bailey:

Sorry for the late response. The reason why it does not work with subplot is that the plotyn function creates number of axes, only one of which is visible. I created a simple workaround for you, which creates the subplots as you want them, stores their positions and then adequately change the positions of the axes holding the plotted graphs.

Hope this helps.

Bailey

Thanks for your willingness to help me Jakub! Maybe my approach to using plotyn with subplot is too simplistic, but here's some code that fails for me:

figure(1)
data(1) = struct('X', 1:100, 'Y', randn(1,100), 'YLabel', 'label1');
data(2) = struct('X', 1:100, 'Y', randn(1,100), 'YLabel', 'label2');
subplot(2,1,1),plotyn(data);
data(1) = struct('X', 1:100, 'Y', randn(1,100), 'YLabel', 'label3');
data(2) = struct('X', 1:100, 'Y', randn(1,100), 'YLabel', 'label4');
subplot(2,1,2),plotyn(data);

Jakub Nedbal

Hello Bailey,
I don't see why it shouldn't work. Try it out and let me know if you fail. I'll try to help you out if you give me a failing example. If I remember correctly, subplot just creates axes in a particular position. Once the axes are made, you should be able to use it with plotyn as you would with other piloting functions

Bailey

Is it possible to use this with subplot?

Jakub Nedbal

In response to Pieterjan De Geest: If I remember correctly, I did not code in support for errorbars. It remains a challenge for others.

What if you want to plot errorbars? Is this possible?

Zhang

Thank you for your reply!
I am using Matlab2013 and assume that it does not have problems in compatibility.In your code description, usage of function ploty4 is mentioned. I considered it as a "review" of other's previous work and ignored it. During the implemetation, the Example in your code is directly copied into anoter script and runned,as following:

x = 0 : 10; data(1).X = x;
data(1).Y = x .^ 1; data(1).YLabel = 'First Y-axis';
data(2).Y = x .^ 2; data(2).YLabel = 'Second Y-axis';
data(3).Y = x .^ 3; data(3).YLabel = 'Third Y-axis';
data(4).Y = x .^ 4; data(4).YLabel = 'Fourth Y-axis';
figure;
[hax, hlines, data] = plotyn(data);
legend(hlines, 'y = x', 'y = x^2', 'y = x^3', 'y = x^4', 2, ...
'Location', 'NorthWest');

I will learn from you and generate codes myself. Thank you for enlightening me.

Jakub Nedbal

Hello Zhang,

You would need to provide more background information in order for me to work out if there is any problem in my code. Pasting an example code would help. I would run it myself and check if it fails.

It seems that you are using a ten year old version of Matlab and the code is simply not backwards compatible. You may need to change "limx = ax.XLim" into "limx = get(ax, 'XLim')". However, if you do that, you will run into similar problem on subsequent lines of code. You would need to fix all the incompatibilities one by one. It can be done quite easily, but you would have to do that yourself.

Zhang

I have a problem running your 4-line plot examlple,would you please check your code again ?

Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure
Variables As Structures Displays Warning, for details.
> In plotyn at 166
In Untitled7 at 8
Reference to non-existent field 'XLim'.

Error in plotyn (line 170)
limx = ax.XLim;

Updates

1.4.0.0

Added updated file with fixed help section.

1.3.0.0

Another refinement of the description.

1.2.0.0

Further improved the description.

1.1.0.0

Fixed the description.

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Acknowledgements

Inspired by: plotyyy, ploty4.m