Azzera filtri
Azzera filtri

Labeling both graphs for a priceandvol plot

1 visualizzazione (ultimi 30 giorni)
Brett Wicker
Brett Wicker il 30 Mar 2021
Modificato: Vaibhav il 14 Feb 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 Commento
Brett Wicker
Brett Wicker il 30 Mar 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')

Accedi per commentare.

Risposte (1)

Vaibhav
Vaibhav il 14 Feb 2024
Modificato: Vaibhav il 14 Feb 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!

Categorie

Scopri di più su Formatting and Annotation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by