Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting data on time (date) axis

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?

like image 482
Rook Avatar asked Jan 10 '10 19:01

Rook


People also ask

How do I set the axis type to date in Plotly?

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.

How to display date and time on x axis in Excel?

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.

How to display the date and time correctly on the chart?

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.

How to plot time series using axes of type date in Python?

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.


2 Answers

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.

like image 99
monksy Avatar answered Oct 18 '22 07:10

monksy


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:

  • Rotate Tick Label by Andrew Bliss
like image 24
gnovice Avatar answered Oct 18 '22 09:10

gnovice