i'm trying to draw a Graph with a user-friendly timeline having every day/week (to be decided by time range) as a label at x-axis. However, the datasource values are given on another basis - there might be 10 entries one day and the eleventh comes in a month.
See the photoshop image:
With the latest Core Plot drop I cannot find a way to do it, need your help.
Regards,
user792677.
The scatter plot asks the datasource for x-y pairs of data. There is no requirement that either value follow some sort of sequence or regular pattern. Other plot types work similarly, although the names and number of data fields can vary.
In your example, return 4 from the -numberOfRecordsForPlot: method. Return the following values from the -numberForPlot:field:recordIndex: method. The table assumes your y-values are calculated by a function f(x), but they can of course come from any source. Use the field parameter to determine whether the plot is asking for the x- or y- value.
Index X-Value Y-Value
0 1 f(1)
1 3 f(3)
2 9 f(9)
3 10 f(10)
Related
Very new to QGIS. I'm using it to analyse agricultural field microplots for research.
I need to give each field plot its own object which I've done so by using the "create grid" tool. But I need to assign a column and row number to each object in the attribute table to make it easier to sort the data. This should start in the bottom left (1:1) and ascend in row for objects above (2:1) and ascend in column for objects to the right (1:2).
I've orientated the grid to start in the bottom left and learned that by using #row_number, I can re-number the objects from 1 to the total number of objects. But I need to add column number and row number to the attribute table.
Is there a simple way of doing this?
I think the most accessible option here is using the field calculator to add two integer columns to the attribute table. For a rectilinear grid with cells_y cells in the y direction (24 in this case) the expressions are something like
floor(("id" - 1) / cells_y) + 1
where id is the attribute table column with the cell number (set automatically by Vector -> Research Tools -> Create grid or, in the OP's case, to #rownumber).
For the y index it's
cells_y + 1 - if(("id" % cells_y) = 0, cells_y, "id" % cells_y)
This is for the ones based indexing asked for here; remove the + 1 bits for zero-based. Similarly, subtracting (as is done with y here) or not subtracting (x here) the modulus part flips the directions in which the cells are numbered. This approach can also be used in PyQGIS.
There's a few grid plugins for QGIS and several similar StackOverflow questions (like this one) which may also be of interest.
I have a series y that contains values, some of which are NaN some numeric (double).
The series has an associated vector d which contains the datenum dates.
Example:
y=[NaN(5,1); rand(10,1)]
d=now-14:now
When I run:
plot(d,y)
I get the graph I want; the NaN observations are taken out.
However, when I run:
plot(d,y); datetick
then my graph starts from the beginning and takes into account all the observations (even when y is a NaN).
How can I prevent this from happening?
From the documentation we can see that there is an easy way (shown below) to preserve the current axes limits.
plot(d,y);
datetick('keeplimits');
The 'keeplimits' argument does exactly what it suggests, maintaining the x-axis limits whilst converting the tick values to dates. You may also want to pass 'keepticks' to preserve tick mark locations.
The behaviour you describe seems contrary to the docs:
datetick selects a label format based on the minimum and maximum limits of the specified axis.
From this statement I would expect the values to remain the same, but there is obviously something about the way the limits are handled internally which means the NaN points are included. At least we are given a simple work around!
I have a set of ages (over 10000 of them) and I want to plot a graph with the age from 20 to 100 on the x axis and then the number of times each of those ages appears in the data on the y axis. I have tried several ways to do this and I can't figure it out. I also have some other data which requires me to plot values vs how many times they occur so any advice on how to do this would be much appreciated.
I'm quite new to Matlab so it would be great if you could explain how things in your answer work rather than just typing out some code.
Thanks.
EDIT:
So I typed histogram(Age, 80) because as I understand that will plot the values in Age on a histogram split up into 80 bars (1 for each age). Instead I get this:
The bars aren't aligned and it's clearly not 1 per age nor has it plotted the number of times each age occurs on the y axis.
You have to use histogram(), and that's correct.
Let's see with an example.
I extract 100 ages between 20 and 100:
ages=randsample([20:100],100,true);
Now I call histogram() in this manner:
h=histogram(ages,[20:100]);
where h is an histogram object and this will also show the following plot:
However, this might look easy due to the fact that my ages vector is in range 20:100, so it will not contain any other values. If your vector, as instead, contains also ages not in range 20:100, you can specify the additional option 'BinLimits' as third input in histogram() like this:
h=histogram(ages,length([20:100]),'BinLimits',[20:100]);
and this option plots a histogram using the values in ages that fall between 20 and 100 inclusive.
Note: by inspecting h you can actually see and/or edit some proprieties of your histogram. An attribute (field) of such object you might be interested to is Values. This is a vector of length 80 (in our case, since we work with 80 bins) in which the i-th element is the number of items is the i-th bin. This will help you count the occurrences (just in case you need them to go on with your analysis).
Like Luis said in comments, hist is the way to go. You should specify bin edges, rather than the number of bins:
ages = randi([20 100], [1 10000]);
hist(ages, [20:100])
Is this what you were looking for?
I'm trying to draw a temperature graph using iso-charts where the x axis data would be set from a server timestamp but the labels would be readable text.
For instance the graph x-axis label would start at Monday 00:00 and end Tuesday 12pm but the LineChartDataSet would be a collection of temperature (y-axis) and timestamps for the x
To display the timestamp I have a custom valueFormatter set as follow (which works great)
lineChartView.xAxis.valueFormatter = timestampXAxisFormatter() //converts timestamp to Date string
My question: The LineChartDataSet seems to be indexed based which is causing some trouble: if I have 4 data points such as (9am, 10), (9:15am, 11), (12pm, 15), (1pm, 16) the 4 points are set in the chart at regular intervals (I was expecting 2 points to be on the left side of the graph and then last 2 points on the right side) - Is there a way to have a data set that is based on the x value instead of the index?
I saw ChartData has an init that takes an array of NSObjects but then it converts it to Strings...Thanks in advance for any suggestions you may have!
There is no good way to solve it, as you figured out the x axis is index based.
You have two options:
insert many x values between each real x value, like between 9:00 and 9:15, you manually insert 9:01, 9:02, ..., 9:14, but don't add any entry at these values, just ignore it and continue. ios-charts will skip if no entry found and go to next. This will works fine, if you don't have a large number of values to insert. I tried ~1000 values, the performance is acceptable.
you create your own chart, using two y axis, one as x axis and one as y axis, so the distances to 0 point are calculated by value. However this requires you understand the ios-chart logic deeply. If you succeed, you are more than welcome to file a PR.
I have been searching various forums for hours but it seems impossible to do a thing in Matlab that's automatic in excel...
I used uiimport to import an xls file with into two arrays (? total newbie), one containing dates for my x-axis and the other the values I want to plot.
I have 180 values. The dates are three dates per month, more or less ranging from May 2008 until now, end of March.
Using
plot(mynumbers)
set(gca,'XTickLabel',dates)
only puts dates for May 2008 on my x-axis!
where did all the other dates go?
Instead using
plot(mynumbers)
set(gca,'XTick',mynumbers,'XTickLabel',dates)
gives error message
"??? Error using ==> set
Values must be monotonically increasing."
Please help!
where did all the other dates go?
The answer to your first question is that MATLAB only uses the first N number of strings corresponding to the default N number of tick marks on the x axis.
"??? Error using ==> set Values must be monotonically increasing."
The error is telling you that your date ticks must be evenly spaced. Instead of using dates corresponding to your actual data points, you could grab the x tick values that MATLAB automatically assigned to your graph, translate them to text, and then reassign the dates as x tick labels, like so:
% generate example unevenly spaced date vector
time = [now,now+1,now+25,now+28.5,now+36,now+40,now+51,now+65];
% generate random data points
data = rand(size(time));
% plot time vs data, storing the axes handle in the process
figure;
axH = axes;
plot(axH,time,data)
% get the x-axis tick locations
ticLoc = get(axH,'XTick');
% format tick labels (substitute any date format you wish)
ticLab = cellfun(#(x) datestr(x,'mm/dd'),num2cell(ticLoc),'UniformOutput',false);
% apply tick labels
set(axH,'XTickLabel',ticLab)
MATLAB's built-in function datetick also performs similarly.
However, if you zoom afterwards, you won't have accurate tick labels. So you may want to use datetick2 on the File Exchange.
If you're having trouble converting a cell array of dates from Excel into a numeric array, use:
dateNumeric = cell2mat(cellfun(#datenum,dateStrings,'UniformOutput',false));
try set (gca,'XTickLabel',num2str(dates))