Labeling both graphs for a priceandvol plot

1 visualización (últimos 30 días)
Brett Wicker
Brett Wicker el 30 de Mzo. de 2021
Editada: Vaibhav el 14 de Feb. de 2024
How can you label the price and volume graphs when you plot the graphs using the priceandvol function? Right now when I enter my code it only attaches to the bottom graph and I do not know how to format the top graph so I can label the x/y axis?
  1 comentario
Brett Wicker
Brett Wicker el 30 de Mzo. de 2021
This is my current code:
ford = xlsread('Ford_Stock_Prices.xlsx');
MATLABDate = x2mdate(ford,1,'datetime');
priceandvol(ford(:,1:end));
xlabel('Date')
ylabel('Volume')
dateaxis('X',12,min(ford(:,1)))
title('Ford Stock Price')
legend('Volume of Shares')

Iniciar sesión para comentar.

Respuestas (1)

Vaibhav
Vaibhav el 14 de Feb. de 2024
Editada: Vaibhav el 14 de Feb. de 2024
Hi Brett
I understand that you are facing issues in formatting the top graph so that you can label the x/y axis.
When you use the priceandvol function to plot stock prices and volume, it creates a figure with two subplots, one on top of the other. The top one usually displays the stock price, while the bottom one shows the trading volume. To add labels and titles to each subplot, you'll need to grab handles to these subplots and then use them to set properties for each one individually.
Here's how you can modify your code to label both the price and volume graphs:
% Read data from Excel file
ford = xlsread('Ford_Stock_Prices.xlsx');
MATLABDate = x2mdate(ford(:,1),1,'datetime');
% Create the price and volume plot
[ax, h1, h2] = priceandvol(ford(:,1:end));
% ax(1) is the handle for the top subplot (price)
% ax(2) is the handle for the bottom subplot (volume)
% Label the x-axis of the bottom plot (volume)
xlabel(ax(2), 'Date')
ylabel(ax(2), 'Volume')
dateaxis(ax(2), 'X', 12, min(ford(:,1)))
title(ax(2), 'Ford Stock Volume')
legend(ax(2), 'Volume of Shares')
% Label the x-axis of the top plot (price)
xlabel(ax(1), 'Date')
ylabel(ax(1), 'Price')
title(ax(1), 'Ford Stock Price')
legend(ax(1), 'Price of Shares')
% Adjusting the x-axis date format for both subplots
dateaxis(ax(1), 'X', 12)
dateaxis(ax(2), 'X', 12)
You can refer to the MathWorks documentation below to learn more about "priceandvol" function:
Hope this helps!

Categorías

Más información sobre Financial Toolbox en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by