Im using line chart from Google Charts. I show date on hAxis (horizontal). My major gridlines show date normally. But there are time labels between date labels. And i dont want time labels.
I tried to put that property in hAxis on chart options but nothing changed
minorGridlines:{textPosition: 'none' }
hAxis options;
hAxis: {titleTextStyle: {color: '#0000ff'},
slantedText:true, slantedTextAngle:60,format: 'd/M/yy',minorGridlines:{textPosition: 'none' },gridlines: {
count: -1,
units: {
days: {format: ['d.MM']},
hours: {format: ['HH:mm']},
}
}
},
is there anyone who knows the solution?
first, remove the options for gridlines.
however, this could cause the same date labels to repeat.
to prevent from repeating,
you can provide your own ticks.
the hAxis.ticks option takes an array, in this case, of dates.
to build the ticks array, we can use data table method --> getColumnRange(colIndex)
this will return the min & max values of the column.
then we can to build our ticks array.
// build hAxis ticks
var hAxisTicks = [];
var dateRange = data.getColumnRange(0);
var oneDay = (24 * 60 * 60 * 1000);
for (var i = dateRange.min.getTime(); i <= dateRange.max.getTime(); i = i + oneDay) {
hAxisTicks.push(new Date(i));
}
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Y');
data.addRows([
[new Date(2019, 0, 1, 0), 10],
[new Date(2019, 0, 1, 6), 20],
[new Date(2019, 0, 1, 12), 30],
[new Date(2019, 0, 1, 18), 40],
[new Date(2019, 0, 2, 0), 10],
[new Date(2019, 0, 2, 6), 20],
[new Date(2019, 0, 2, 12), 30],
[new Date(2019, 0, 2, 18), 40],
[new Date(2019, 0, 3, 0), 10],
[new Date(2019, 0, 3, 6), 20],
[new Date(2019, 0, 3, 12), 30],
[new Date(2019, 0, 3, 18), 40],
[new Date(2019, 0, 4, 0), 10],
[new Date(2019, 0, 4, 6), 20],
[new Date(2019, 0, 4, 12), 30],
[new Date(2019, 0, 4, 18), 40],
]);
// build hAxis ticks
var hAxisTicks = [];
var dateRange = data.getColumnRange(0);
var oneDay = (24 * 60 * 60 * 1000);
for (var i = dateRange.min.getTime(); i <= dateRange.max.getTime(); i = i + oneDay) {
hAxisTicks.push(new Date(i));
}
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {
hAxis: {
format: 'd/M/yy',
ticks: hAxisTicks
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
EDIT
building the ticks manually will cause a performance hit,
but only by a couple milliseconds, which is nothing.
see following working snippet,
540 rows are drawn, the milliseconds it takes to build the ticks are shown in the console...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Y');
for (var i = (new Date(2019, 0, 1)).getTime(); i <= (new Date(2019, 3, 1)).getTime(); i = i + (4 * 60 * 60 * 1000)) {
data.addRow([new Date(i), i]);
}
// build hAxis ticks
var test = new Date();
console.log('rows', data.getNumberOfRows());
var hAxisTicks = [];
var dateRange = data.getColumnRange(0);
var oneDay = (24 * 60 * 60 * 1000);
for (var i = dateRange.min.getTime(); i <= dateRange.max.getTime(); i = i + oneDay) {
hAxisTicks.push(new Date(i));
}
console.log('milliseconds', ((new Date()).getTime() - test.getTime()));
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {
hAxis: {
format: 'd/M/yy',
ticks: hAxisTicks
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
Related
This is my code for sobel filter:
def init_f(shape, dtype=None):
sobel_x = tf.constant([[-5, -4, 0, 4, 5], [-8, -10, 0, 10, 8], [-10, -20, 0, 20, 10], [-8, -10, 0, 10, 8], [-5, -4, 0, 4, 5]])
ker = np.zeros(shape, dtype)
ker_shape = tf.shape(ker)
kernel = tf.tile(sobel_x, ker_shape)//*Is this correct?*
return kernel
model.add(Conv2D(filters=30, kernel_size=(5,5), kernel_initializer=init_f, strides=(1,1), activation='relu'))
So far I have managed to do this.
But, this gives me error:
Shape must be rank 2 but is rank 4 for 'conv2d_17/Tile' (op: 'Tile') with input shapes: [5,5], [4].
Tensorflow Version: 2.1.0
You're close, but the args to tile don't appear to be correct. That is why you're getting the error "Shape must be rank 2 but is rank 4 for..." You're sobel_x must be a rank 4 tensor, so you need to add two more dimensions. I used reshape in this example.
from tensorflow import keras
import tensorflow as tf
import numpy
def kernelInitializer(shape, dtype=None):
print(shape)
sobel_x = tf.constant(
[
[-5, -4, 0, 4, 5],
[-8, -10, 0, 10, 8],
[-10, -20, 0, 20, 10],
[-8, -10, 0, 10, 8],
[-5, -4, 0, 4, 5]
], dtype=dtype )
#create the missing dims.
sobel_x = tf.reshape(sobel_x, (5, 5, 1, 1))
print(tf.shape(sobel_x))
#tile the last 2 axis to get the expected dims.
sobel_x = tf.tile(sobel_x, (1, 1, shape[-2],shape[-1]))
print(tf.shape(sobel_x))
return sobel_x
x1 = keras.layers.Input((128, 128, 3))
cvl = keras.layers.Conv2D(30, kernel_size=(5,5), kernel_initializer=kernelInitializer, strides=(2,2), activation='relu')
model = keras.Sequential();
model.add(x1)
model.add(cvl)
data = numpy.ones((1, 128, 128, 3))
data[:, 0:64, 0:64, :] = 0
pd = model.predict(data)
print(pd.shape)
d = pd[0, :, :, 0]
for row in d:
for col in row:
m = '0'
if col != 0:
m = 'X'
print(m, end="")
print("")
I looked at using expand_dims instead of reshape but there didn't appear any advantage. broadcast_to seems ideal, but you still have to add the dimensions, so I don't think it was better than tile.
Why 30 filters of the same filter though? Are they going to be changed afterwards?
My x-axis for google visualisation chart is javascript new Date(year+i, 0, 0). However when I print the line chart. It shows M J S at the bottom with the year number.
How can I remove these M J S (which I presume are May June and September).
the following snippet re-produces the problem, with year number combined with month abbreviations...
2020 M J S 2021 M J S etc...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Mutual Fund');
for (var y = 2020; y < 2024; y++) {
for (var m = 0; m < 12; m++) {
data.addRow([new Date(y, m, 1), (10000 + y + m)]);
}
}
var container = document.getElementById('chart');
var chart = new google.visualization.LineChart(container);
chart.draw(data);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
to only show year, you can use hAxis option format.
hAxis: {
format: 'yyyy'
}
however, this could cause the year to repeat (depending on the width of the chart)...
2020 2020 2020 2020 2021 2021 2021
see following snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Mutual Fund');
for (var y = 2020; y < 2024; y++) {
for (var m = 0; m < 12; m++) {
data.addRow([new Date(y, m, 1), (10000 + y + m)]);
}
}
var container = document.getElementById('chart');
var chart = new google.visualization.LineChart(container);
chart.draw(data, {
hAxis: {
format: 'yyyy'
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
to ensure the year doesn't repeat, we must also provide hAxis option ticks.
ticks must be in the same format as the data on the x-axis, in this case a date.
so we provide the jan date for each year...
hAxis: {
format: 'yyyy',
ticks: [new Date(2020, 0, 1), new Date(2021, 0, 1), new Date(2022, 0, 1)]
}
you should be able to create the ticks dynamically, from the data.
see following snippet for an example...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Mutual Fund');
var ticks = [];
for (var y = 2020; y < 2024; y++) {
for (var m = 0; m < 12; m++) {
data.addRow([new Date(y, m, 1), (10000 + y + m)]);
if (m === 0) {
ticks.push(new Date(y, m, 1));
}
}
}
var container = document.getElementById('chart');
var chart = new google.visualization.LineChart(container);
chart.draw(data, {
hAxis: {
format: 'yyyy',
ticks: ticks
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
I wondered if it was possible to make the circle, that appears once hovered across the Google line chart, have a coloured border?
you can use a style column role to change the style of a point.
point {stroke-width: 2; stroke-color: red;}
see following working snippet,
hover a point to see the style...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'y');
data.addColumn({role: 'style', type: 'string'});
data.addRows([
[0, 1, 'point {stroke-width: 2; stroke-color: red;}'],
[1, 2, 'point {stroke-width: 2; stroke-color: red;}'],
[2, 3, 'point {stroke-width: 2; stroke-color: red;}'],
[3, 4, 'point {stroke-width: 2; stroke-color: red;}'],
[4, 5, 'point {stroke-width: 2; stroke-color: red;}'],
]);
var options = {
legend: 'none'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
My data looks like this:
A B
3 5000
2 6218
4 9435
I need to create a chart where A is the number of points/block size and B is the Value of each point.
For example, the date above will create a line graph where the 2 first points will have the value of 5000, the next 2 points will have the value of 6218, and the next 4 points will have the value of 9435.
The graph I need will look like the automatic one that will be generated for this data:
5000
5000
5000
6218
6218
9435
9435
9435
9435
use the original data table to build a new data table.
for each row in the original table, add a row to the new table however many times 'A' represents.
var x = 0;
for (var row = 0; row < rawData.getNumberOfRows(); row++) {
for (var a = 0; a < rawData.getValue(row, 0); a++) {
chartData.addRow([x, rawData.getValue(row, 1)]);
x++;
}
}
see following working snippet...
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var rawData = google.visualization.arrayToDataTable([
['A', 'B'],
[3, 5000],
[2, 6218],
[4, 9435]
]);
var chartData = new google.visualization.DataTable();
chartData.addColumn('number', 'X');
chartData.addColumn('number', 'Y');
var x = 0;
for (var row = 0; row < rawData.getNumberOfRows(); row++) {
for (var a = 0; a < rawData.getValue(row, 0); a++) {
chartData.addRow([x, rawData.getValue(row, 1)]);
x++;
}
}
var container = document.getElementById('chart_div');
var chart = new google.visualization.LineChart(container);
chart.draw(chartData, {
height: 288,
pointSize: 4,
vAxis: {
minValue: 0
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
I have a matrix of the form:
m = 1, 0, 10, 20, 30, 40, 50;
2, 1, 11, 20, 30, 40, 50;
3, 0, 12, 20, 30, 40, 50;
4, 1, 12, 21, 30, 40, 50;
For a given column index (say 3) and row index (say 1), I'd like to filter out all rows that have the same values to the right of that column in that row. Using the above example of m, columnIndex = 3, and rowIndex = 1 (noted with asterisks):
**
f(m, 3) = * 1, 0, 10, 20, 30, 40, 50; % [20, 30, 40, 50] matches itself, include
2, 1, 11, 20, 30, 40, 50; % [20, 30, 40, 50] matches the subvector in row 1, include
3, 0, 12, 20, 30, 40, 50; % [20, 30, 40, 50] matches the subvector in row 1, include
4, 1, 12, 21, 30, 40, 50; % [21, 30, 40, 50] does NOT match the subvector in row 1, filter this out
How can I achieve this behavior? I've tried this, but I'm getting an dimension mismatch error.
key = data( rowIndex, columnIndex:end );
filteredData = ( data( :, columnIndex:end ) == key );
Index those that you keep with == within bsxfun():
r = 3;
c = 2;
idx = all(bsxfun(#eq, m(:,c:end),m(r,c:end)),2);
m(idx,:)
I think you're looking to use the isequal operator, documented here.
isequal(m(1,columnIndex:end),key)
and here's kind of an inefficient one liner :-)
cellfun(#(x) isequal(key,x),mat2cell(m(:,columnIndex:end),ones(1,size(m,2)-columnIndex+1)))
Here's how it goes
Change the matrix to a cell array of the subvectors we're interested in: mat2cell(m(:,columnIndex:end),ones(1,size(m,2)-columnIndex+1))
Anonymous function to run on each cell element: #(x) isequal(key,x)
Function to cause each cell element to be passed to the anonymous function, cellfun
My answer using m above as an example:
cellfun(#(x) isequal(key,x),mat2cell(m(:,columnIndex:end),ones(1,size(m,2)-columnIndex+1)))
ans =
1
1
1
0
HTH!