Labeling both graphs for a priceandvol plot
이전 댓글 표시
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개)
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!
카테고리
도움말 센터 및 File Exchange에서 Financial Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!