bar plot with two y-axis -1e3 in matlab

You can create a bar plot with two y-axis in MATLAB by using the yyaxis function. Here is an example code snippet that demonstrates how to do it:

main.m
% Generate some sample data
x = 1:5;
y1 = [1 2 3 4 5];
y2 = [-1 -2 -3 -4 -5];

% Create a bar plot
bar(x, y1)

% Add a second y-axis
yyaxis 'right'

% Create a line plot using the second y-axis
plot(x, y2);

% Set the labels for the y-axes
ylabel('y1')
yyaxis 'right'
ylabel('y2')

% Set the limits for the y-axes
ylim([-1e3, 1e3])
yyaxis 'right'
ylim([-1e3, 1e3])
365 chars
24 lines

In this example, the bar function creates a bar plot using the first y-axis (y1). Then, the yyaxis function is used to switch to the right y-axis, and the plot function is used to create a line plot using the second y-axis (y2). The ylabel function is used to set the labels for each y-axis, and the ylim function is used to set the limits for each y-axis.

Please note that the yyaxis function is available in MATLAB R2016b or later versions. In earlier versions, you can use the plotyy function to create a dual y-axis plot.


tags: matlab, plot, bar plot, y-axis, dual y-axis

gistlibby LogSnag