Put the name in the "x axis" of the graph bar every five days or seven days

1 visualizzazione (ultimi 30 giorni)
Hi, i hope all of you have a wonderful day, i have the matlab R2019a, and i want a simply thing, put the name of the days every five days or seven days to dont overlap the names of the days, because i can't see what day it is. I think is a very simple thing , but i can't find how to do it. i want to find the way to is easy to see the names
Thanks in advance..
Ana S
This is my code:
clear;clc;
numData = xlsread('enerosolar.xlsx','EneDic')
Dia = numData(:,1);
Rad = numData(:,2);
figure;
bar(Dia,'b')
xlabel('Día de Julio 2014')
ylabel('Irradiación Solar Global [KWh/m^2]');
xticks(1:1:length(Rad)) % This will set how many ticks you want on the x-axis. Here, there should be 48 ticks being generated. One for each piece of data you have.
xtickangle(90) % This will rotate the label so that the labels will not overlap with one another. This is in degrees.
xlim([0:5:31])
% perc = hline(4.43,'r', 'Días parcialmente nublados'); hold on

Risposta accettata

dpb
dpb il 12 Lug 2020
Modificato: dpb il 12 Lug 2020
Don't put the ticks every day; that's the cause for there being that many--you did it to yourself! :)
The bar() command will put ticks at convenient places automagically, if you don't like those you can then put them where want.
Use xticklabel to write something besides the tick value.
If you would convert to use datetime, you could get the day string for free...
ADDENDUM:
Sorry, got sidetracked and didn't get back when thought would/could...
It's relatively simple to use dates for the x axis and then the datetime ruler object gives you a lot of flexibility for dates --I'll also demonstrate the new(ish) MATLAB table object in lieu of the old xlsread route...
tEnersol=readtable('enerosolar.xlsx','Sheet','EneDic'); % read into a table object
tEnersol.Properties.VariableNames={'Rad','Dia'}; % assign variable names
yr=2014; % need the proper year
tEnersol.Date=datetime(yr,1,tEnersol.Dia); % and create a date column
hB=bar(tEnersol.Date,tEnersol.Rad); % bar plot against actual date
hAx=gca; % get axes handle...
xlim([datetime(2014,1,1) datetime(2015,1,1)]) % set limit to cover the full year
hAx.XAxis.TickLabelFormat='eee'; % show day of week
produced
which you'll note doesn't have the same shape as yours as it is plotted using the actual date in the data file on the x axis, not against ordinal position in the file (which is not ordered by date). This will show up if change the time format on the x-axis to 'MMM'
which illustrates the data must be from somewhere in the northern hemisphere.
  6 Commenti
Ana Soph
Ana Soph il 13 Lug 2020
well, yes is make sense but i have something like that, but i want in the x axis the real name of the days every five days, like this (the first photo) but this is make it in excel.
My code right know is like this
clear;clc;
numData = xlsread('enerosolar.xlsx','EneDicS')
Rad = numData(:,1);
Dia = numData(:,2);
figure; bar(Rad,'b'), xlabel('Dia del año'), ylabel('Irradiación Solar Global [KWh/m^2]');
perc = hline(9,'r', 'Días de cielo despejado y Parcialmente despejado'); hold on
perc = hline(5.5,'r', 'Días Parcialmente nublados'); hold on
perc = hline(4,'r', 'Días nublados'); hold on

Accedi per commentare.

Più risposte (1)

dpb
dpb il 13 Lug 2020
Well, if you say so... :)
tEnersol=readtable('enerosolar.xlsx','Sheet','EneDic'); % read into a table object
tEnersol.Properties.VariableNames={'Rad','Dia'}; % assign variable names
yr=2014; % need the proper year
tEnersol.Date=datetime(yr,1,tEnersol.Dia); % and create a date column
tEnersol.Date.Format='MMMdd'; % display date format
hB=bar(tEnersol.Rad); % bar plot in descending order vs ordinal position
hAx=gca; % get axes handle...
xticks(1:14:365) % about all the ticks there's room for...
xticklabels(cellstr(tEnersol.Date(xticks))) % use the date string as tick labels
hAx.XTickLabelRotation=45; % rotate so can have chance to read...
If you have arbitrary locations to identify, it's the same idea; just have to look up what the index/ordinal number is of the wanted locations and put tick and ticklabel at those positions.
  3 Commenti
Ana Soph
Ana Soph il 13 Lug 2020
Modificato: Ana Soph il 13 Lug 2020
Oh. I know. I wasn't being clear and i will remember your instructions, again thank you for you patience and for explain to me every thing. This was so educative.i hope you have an amazing Day. Best!

Accedi per commentare.

Categorie

Scopri di più su Dates and Time 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