Im currently plotting a bar graph and a curve with float intervals as months, displayed as 2012.083333, 2012.16666667 etc. etc. on the x-axis.
Im plotting a 4 year interval and want to increase the spacing between the months and cant find a solution in the holoviews documentation.
these are my options and object coding at the moment:
% output size = 250
% opts Curve (color='red')
% opts Curve.food (color='green')
%opts Curve (linewidth=3.0)
# holoviews objects
bars = hv.Bars(data_inject1, "{}".format(country), 'normalised precipitation and temperature, product: {}'.format(product), label = 'precipitation')
line1 = hv.Curve(data_inject2, "{}".format(country), 'normalised precipitation and temperature, product: {}'.format(product),label = 'temperature')
line2 = hv.Curve(data_inject3, label = 'food')
# merge hv objects
plot = bars * line1 * line2
So the question is effectively, how do I increase the x-axis spacing?
Kind regards, Take
Something like this?
hv.Curve((np.arange(0, 10))).options(xticks=[0, 8])
where xticks can be np.arange(2012, 2018, 1) or something.
holoviews/bokeh/matplotlib also support datetime formatting if you don't want to deal with float intervals
Related
I try to plot unemployment over time but years are displayed in 10 year frequency. I want to have x-axis ticks every 5 years but can't seem to manage this as xticks are not in dateformat
Below is the relevant code if you want to take a look.
clear;
X=xlsread('UNRATE.xls');%Import Data
s_year=1950;
e_year=2015;
X=X((s_year-1948)*12+1:(e_year-1948)*12+12);
startdate = datenum('01-1950','mm-yyyy');
enddate = datenum('12-2015','mm-yyyy');
dt = linspace(startdate,enddate,792);%create numeric dates
plot(dt,X);%plot data against dates
datetick('x','yyyy');%format dates in display
recessionplot;
title('Unemployment Rate in the US over time');
xlabel('Year'); % x-axis label
ylabel('Unemployment Rate'); % y-axis label
When working with time series, the best you can do is to manually modify the XTick property of the axis so that it fits your needs once the plotting has been performed. For example:
%...
plot(dt,X);
set(gca,'XTick',datenum(1950:5:2015,1,1));
%...
Working example:
clear;
s_year=1950;
e_year=2015;
X= rand(792,1);
startdate = datenum('01-1950','mm-yyyy');
enddate = datenum('12-2015','mm-yyyy');
dt = linspace(startdate,enddate,792);
xticks = datenum(1950:5:2015,1,1);
xlabels = cellstr(num2str(year(xticks.')));
figure();
plot(dt,X);
set(gca,'XTick',xticks);
set(gca,'XTickLabel',xlabels);
Output:
Original plot without ticks adjustement:
Everyone wants to get rid of time gaps in plot, I want them to be shown!
So I have a table in my workspace (tableM) with two variables (Date and Temperature).
Date is in the "datenum" format. I have done a lot of preprocessing on the data and had to remove many rows. Thus, I have ended up with a date variable with gaps.
When I plot the Temperature through this code: plot(tableM.Temperature), MATLAB plots the temperature normally and it just connects the two points (before the missing date with after the missing date).
When I plot temperature through this code plot(tableM.Date,tableM.Temperature) and use the datetick function, I get a plot where the date is fixed in the horizontal axis and for the date gaps, MATLAB connects the two points (before and after the missing date) with a straight line over this gap period.
What I want is to get zero for the gap period instead of this straight line connecting these points.
How do I do this?
This is the example:
% Generating data:
Date = [datenum(now-2000):datenum(now+2000)]';
Temperature = rand(4001,1); Temperature = sort(Temperature);
% Creating gaps:
Date(500:600) = []; Temperature(500:600) = [];
Date(500:600) = []; Temperature(500:600) = [];
Date(1500:2000) = []; Temperature(1500:2000) = [];
Date(1500:2000) = []; Temperature(1500:2000) = [];
% To make illustration mroe understandable:
Temperature(601:1499) = 1.8 * Temperature(601:1499);
% Normal plot:
figure; plot(Date,Temperature)
ax = gca; ax.XTickLabelRotation = -45; ax.XTick = Date(1,1):200:Date(end,1);
datetick('x',20,'keepticks','keeplimits'); ylabel('Temperature (C)'); axis tight
% Scatter plot:
figure; scatter(Date,Temperature,'.')
ax = gca; ax.XTickLabelRotation = -45; ax.XTick = Date(1,1):200:Date(end,1);
datetick('x',20,'keepticks','keeplimits'); ylabel('Temperature (C)'); axis tight
IMPORTANT: My "Date" vector is on "hourly" basis. I don't know if that creates any particular difference between the datenum values in the Date vector that could be useful.
Image1:
Normal and Scatter Plots
Image2:
Preferred Plot
I have a (1x700) vector x for which I would like to create and plot a time series object in Matlab. Each observation corresponds to one month, and the first observation belongs to January 1960. I tried the following:
state1 = timeseries(x,1:size(x,2));
state1.Name = 'Test';
state1.TimeInfo.Units = 'months';
state1.TimeInfo.StartDate = 'Jan-1960'; % Set start date.
state1.TimeInfo.Format = 'yy'; % Set format for display on x-axis.
state1.Time = state1.time - state1.time(1); % Express time relative to the start date.
plot(state1);
However, I still see numbers on the x-axis instead of years. Could anyone please help? Thanks in advance!
Create random data. 1/12 corresponds to the fraction of a year that each month represents.
x = 1960:1/12:1970;
y = rand(1,121);
Then plot the x and y axes data using plot.
plot( x, y )
Then set the tick as follows for a decade per year. 1960:1970 will generate [1960 1961 ...] each corresponding to the tick's year.
set( gca, 'XTick', 1960:1970 );
Here is the output plot.
Doing 1 year intervals get VERY MESSY with lots of data. So solutions include doing a larger interval or setting your ticks to display vertically instead of horizontally. This code below shows how to set 5 year intervals instead.
set( gca, 'XTick', 1960:5:2010 );
I'm creating a bar plot in Matlab 2014b and would like to center the x-axis labels in between the tick marks. For example, in the following graph, the bars are correctly segmented by year using datetick and a slight adjustment I make. However, I would like the labels to appear half way in between the tick marks that are currently specified.
clear; close all;
a = rand(12, 1)-0.1;
x = linspace(datenum('03-31-2012'), datenum('12-31-2014'), 12);
b1 = bar(x, a);
datetick('x', 'yyyy');
display(datestr(x))
ax1 = gca;
bGapWidth = (x(2)-x(1));
bWidth = b1.BarWidth;
ax1.XLim = [x(1)-bGapWidth*bWidth x(end)+bGapWidth*bWidth];
initTick = ax1.XTick;
ax1.XTick = initTick + (bWidth*bGapWidth)/2 + (bGapWidth*(1-bWidth)/2);
I've seen a few similar questions but nothing quite the same. I've also seen suggestions of creating a dummy axis (one for labels, one for tick marks), but I've had some trouble here too - simply setting a new variable equal to the current axes object and making modifications changes the whole plot. Feel free to help me out on this part, or suggest a better solution in general.
clear; close all;
a = rand(12, 1)-0.1;
x = linspace(datenum('03-31-2012'), datenum('12-31-2014'), 12);
b1 = bar(x, a);
datetick('x', 'yyyy');
display(datestr(x))
h1=get(gca,'XTick');
h2=get(gca,'XTickLabel');
h3=length(h1);
xdiff=h1(2)-h1(1); % assuming uniform step interval in x-axis
h1=h1+0.4*xdiff; % this factor of 0.4 can be adjusted
ylim([0 1]);
for i=1:h3-1
text(h1(i),-0.05,num2str(h2(i,:)));
end
% instead of -0.05 relative to y put the labels where you like
set(gca,'XTickLabel',{});
I'd like to create a quasi boxplot graph as shown on pages 15/16 of the attached report.
comisef.eu/files/wps031.pdf
Ideally I only want to show the median, the maximum and minimum values as in the report.
I would also like to have similar spacing to that shown in the report.
Currently I have two matrices with the all the necessary values stored in them but have no idea how to do this in matlab.
The boxplot function gives too much data (outliers etc) which makes the resulting graph look confused especially when I try to plot 200 on one page as in the original report.
Is there another function that can so the same thing as in the report in matlab?
Baz
OK here is some test data each row represents 10 sets of estimations of a data set, and each column represents the test number for a given observation.
As boxplot works on the columns of the input matrix you will need to transpose the matrix.
Is it possible to turn outliers and the inter-quartile ranges off? Ideally I just want to see the maximum, minimum and median values?
You can repeat the data below to get up to 200. Or I can send more data if necessary.
0.00160329732202511 0.000859407819412016 0.000859407819411159 0.0659939338995606 0.000859407819416322 0.000859407819416519 2.56395024851142e-15 2.05410662537078e-14 0.000859407819416209
1.67023155116586e-06 8.88178419700125e-16 1.67023155115637e-06 0.000730536218639616 1.67023155105582e-06 3.28746017489609e-15 4.41416632660789e-15 1.67023155094400e-06 1.67023155097567e-06
1.42410590843629e-06 1.42410590840224e-06 1.76149166727218e-15 5.97790925044131e-15 1.42410590843863e-06 2.87802701599909e-15 9.31529385335274e-16 9.17306727455842e-16 0.000820358763518906
8.26849110292527e-16 3.23505095414772e-15 4.38139485761850e-07 4.38139485938112e-07 4.38139485981887e-07 0.000884647755317917 3.72611754134110e-15 4.38139485974329e-07 4.38139485923219e-07
0.000160661751819407 0.000870787937135265 0.000870787937136209 1.16934122581182e-15 9.02860049358913e-16 1.18053134896556e-15 1.40433338743068e-15 0.000870787937135929 1.13510916297112e-15
1.16934122581182e-15 3.80292342262841e-05 3.80292342263200e-05 0.00284904319356532 1.74649997619656e-15 3.80292342264024e-05 0.00284904319356537 1.01267920724547e-15 0.00284904319356540
0.100091800399985 0.100091773169254 0.100091803903140 0.000770464183529358 0.100091812455930 3.49996706323281e-05 3.49996706323553e-05 1.05090687851466e-15 0.100091846333800
0.00100555294602561 0.00100555294601056 0.105365907420183 0.000121078082591672 9.02860049358913e-16 0.000121078082591805 4.49679158258033e-15 7.77684615168284e-16 0.000121078082591693
0.122539456858702 0.000363547764643498 0.000363547764643509 0.122516928568610 0.0101487499394213 0.122408366511784 0.000363547764643519 1.13510916297112e-15 0.122521393586646
0.000460749357561036 0.000460749357560646 3.27600489447913e-13 1.18053134896556e-15 0.000460749357561239 1.54689304063675e-15 0.000460749357560827 0.000460749357561205 1.16934122581182e-15
Instead of using boxplot, I suggest just drawing lines from the min to the max and making a mark at the median. Boxplot draws boxes from the 25 to 75 percentile, which doesn't sound like what you want. Something like this:
% fake data
nPoints = 100;
data = 10*rand(10, nPoints);
% find statistics
minData = min(data, [], 1);
maxData = max(data, [], 1);
medData = median(data);
% x coordinates of each line. Change this to change the spacing.
x = 1:nPoints;
figure
hold on
%plot lines
line([x; x], [minData; maxData])
% plot cross at median
plot(x, medData, '+')
EDIT: To have horizontal lines and a second axis you can do something like this:
figure
h1 = subplot(1,2,1);
h2 = subplot(1,2,2);
% left subplot
axes(h1)
hold on
%plot lines
line([minData; maxData], [x; x])
% plot cross at median
plot(medData, x, '+')
% link the axes so they will have the same limits
linkaxes([h1,h2],'y')
% turn off ticks on y axis.
set(h2, 'YTick', [])
I think it's a question of playing with the settings. You can try:
boxplot(X, 'plotstyle', 'compact', 'colors', 'k', 'medianstyle', 'line', 'outliersize', 0);
Explanation:
'plotstyle', 'compact': makes the boxes filled and the lines undashed
'colors', 'k': color is black
'medianstyle', 'line': the median is marked by a line
'outliersize', 0: if outlier size is zero, you don't see them
Other you can try:
'orientation', 'vertical': this flips the orientation, depends on your data
'whisker', 10 (or higher): this sets the maximum whisker length as a function of the interquartile limits (if you crank it up, it will eventually default to max and min values), I wasn't sure if this is what you wanted. Right now, it goes to the 25th and 75th percentile values.
The spacing is going to depend on how much data you have. If you edit with some data, I can try it out for you.