I have data like this:
22.10.1980. 100
25.10.1980. 120
26.10.1980. 12
(only much more of it, and for each date, several independent measurements on the right).
Now, this is probably trivial, but I've never done anything like it in MATLAB, and am having problems finding similar examples online. I need to plot the data on a time/showing dates axis (x axis), with all dates inside (so, 23. and 24. as well ... for which I don't have measurements).
How can I get dates to show up on a plot axis?
Plotly auto-sets the axis type to a date format when the corresponding data are either ISO-formatted date strings or if they're a date pandas column or datetime NumPy array. Dash is the best way to build analytical apps in Python using Plotly figures.
If you use the excel version later than 2010, you can see a Format Axis pane pops out, and check Text axis option under Axis Type in the Axis Options group. 3. Click Close or go back to the chart, then then date and time data has been correctly display in the X axis.
To display the date and time correctly, you only need to change an option in the Format Axis dialog. 1. Right click at the X axis in the chart, and select Format Axis from the context menu.
Time Series using Axes of type date Time series can be represented using either plotly.express functions (px.line, px.scatter, px.bar etc) or plotly.graph_objects charts objects (go.Scatter, go.Bar etc). For more examples of such charts, see the documentation of line and scatter plots or bar charts.
It seems like it might be the best to use datetick
.
Usage: datetick('x')
or datetick('x', dateformat)
with the available formats as explained in the documentation.
Assuming your data file has the format given above, you could use textscan
to read the data:
fid = fopen('data.txt','rt');
C = textscan(fid,'%s %s %s %d','Delimiter','.','CollectOutput',1);
fclose(fid);
The first cell of C
will contain an N-by-3 cell array of strings (the parts of the date) and the second cell of C
will contain an N-by-1 vector of the data measurements. You can create a date number for each measurement by first concatenating the 3 smaller strings into one date string and then using the datenum
function:
t = datenum(strcat(C{1}(:,3),'-',C{1}(:,2),'-',C{1}(:,1)));
data = C{2};
Once you have a vector of date numbers t
to go with your vector of measurements data
, you can then plot them:
plot(t,data,'*'); %# Plot the points as asterisks
Now, you can change the x-axis labels to show the actual dates. One option is to use the function datetick
, an easy and elegant solution given in steven's answer. Another option is to use the function datestr
to create the labels yourself, then modify the XTick
and XTickLabel
properties of the current axes:
xpts = min(t):max(t); %# Make a full vector, filling in missing dates
set(gca,'XTick',xpts,'XTickLabel',datestr(xpts)); %# Set axes properties
NOTE: Whichever option you choose for changing the x-axis labels to date strings, you may run into trouble with the labels overlapping each other if the tick marks are too close together. You could fix this by reducing or repositioning the tick marks along the x-axis (by changing the XTick
property) or by adjusting the axes FontSize
property. If you wanted to rotate the labels to make them fit, you would have to erase the labels and create new rotated text objects for them. The following submission on The MathWorks File Exchange does just that:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With