Labeling both graphs for a priceandvol plot

1 次查看(过去 30 天)
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 个评论
Brett Wicker
Brett Wicker 2021-3-30
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')

请先登录,再进行评论。

回答(1 个)

Vaibhav
Vaibhav 2024-2-14
编辑:Vaibhav 2024-2-14
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!

类别

Help CenterFile Exchange 中查找有关 Formatting and Annotation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by