Grid line consistent with ticks on axis
From ?grid
description of the nx
and ny
arguments:
When NULL, as per default, the grid aligns with the tick marks on the
corresponding default axis (i.e., tickmarks as computed by axTicks)
plot (x = 1:10, y = rnorm (10, 5, 2))
grid (NULL,NULL, lty = 6, col = "cornsilk2")
How to show tick marks and labels with grid line for the specific one on the X axis with Chart.js?
use this for your labels array:
labels: [["jan", 20], "", "", "", "", "", ["jul", 20], "", "", "", "", "", ["jan", 21]],
and this to your options object:
options: {
scales: {
xAxes: [{
ticks: {
callback: (label) => (label === "" ? null : label)
}
}]
}
}
will get the following result:
Since the tick marks are getting generated from the grid lines they dissapear too, dont know how you can fix that
How to make axes ticks in between grid lines in matplotlib?
You can set the minor ticks so that only 1 minor tick appears inbetween your major ticks. This is done using matplotlib.ticker.AutoMinorLocator
. Then, set the gridlines to only appear at the minor ticks. You also need to shift your xtick positions by 0.5:
from matplotlib.ticker import AutoMinorLocator
np.random.seed(10)
x = range(10)
y = np.random.random(10)
plt.plot(x,y)
plt.xticks(np.arange(0.5,10.5,1), x)
plt.xlim(0,9.5)
plt.ylim(0,1)
minor_locator = AutoMinorLocator(2)
plt.gca().xaxis.set_minor_locator(minor_locator)
plt.grid(which='minor')
plt.show()
Edit: I'm having trouble getting two AutoMinorLocator
s to work on the same axis. When trying to add in another one for the y axis, the minor ticks get messed up. A work around I have found is to manually set the locations of the minor ticks using a matplotlib.ticker.FixedLocator
and passing in the locations of the minor ticks.
from matplotlib.ticker import AutoMinorLocator
from matplotlib.ticker import FixedLocator
np.random.seed(10)
x = range(10)
y = np.random.random(10)
plt.plot(x,y)
plt.xticks(np.arange(0.5,10.5,1), x)
plt.yticks([0.05,0.15,0.25,0.35,0.45,0.55,0.65,0.75,0.85,0.95,1.05], [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
plt.xlim(0,9.5)
plt.ylim(0,1.05)
minor_locator1 = AutoMinorLocator(2)
minor_locator2 = FixedLocator([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
plt.gca().xaxis.set_minor_locator(minor_locator1)
plt.gca().yaxis.set_minor_locator(minor_locator2)
plt.grid(which='minor')
plt.show()
How to draw grid lines fir every ticks?
Drawing grid lines can be done using the tickSizeInner method of the axis. You don't need to specifically draw the grid lines as you do in your code.
Example:
d3.axisBottom(xScale).tickSizeInner(-height)
In your case, to include the padding, the above will change to:
d3.axisBottom(xScale).tickSizeInner(-(h-padding))
Snippet:
const dataSet = [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
const w = 1200;const h = 250;const padding = 20;const paddingYaxis = 50;
const xScale = d3.scaleLinear() .domain([0,100]) .range([paddingYaxis, w - 10]);
const svg = d3.select('#costSquareGraph');
const xAxis = d3.axisBottom(xScale) .tickSizeInner(-(h-padding)) .tickPadding(5);
svg.append('g') .attr('transform', 'translate(0,' + (h - padding) + ')') .call(xAxis); //.selectAll('.tick line').attr('y1', 3);
svg { width: 100%;
}
.tick line { stroke: #CCC;}.cost-square-wrap { width: 100%;}
<script src="https://d3js.org/d3.v5.min.js"></script>
<div class="cost-square-wrap"> <svg id="costSquareGraph" viewbox="0 0 1200 250"></svg> </div>
Align grid with ticks
Looks like abline
is the answer:
mytime <- as.POSIXct("2015-08-20") + seq(0,by=3600,length.out=7*24)
plot(x=mytime,y=rnorm(7*24),xaxt="n")
ticks <- c(mytime[seq(1,by=12,to=length(mytime))],mytime[1]+7*24*3600)
axis(1, ticks, strftime(ticks, "%a %H:%M"))
grid(ny=NULL,nx=NA)
abline(v=ticks[seq(1,length(ticks),2)],lty="dotted",col="lightgray")
D3.js y- axis ticks and grid lines do not align
I created a fiddle.
that while I had to change a few things to account for no angular, but shows the axis gridlines and labels aligned. I moved everything into one g
group is is then translated and the individual components are no longer translated.
Align grid() to plot ticks
You could use abline
to draw grids. You can specify where the grids should be with h
(for horizontal lines) and v
(for vertical lines)
#Plot
plot(1:10,las=1,xaxp = c(0, 10, 10),xlim=c(0,10), ylim=c(0,10))
#Add horizontal grid
abline(h = c(0,2,4,6,8,10), lty = 2, col = "grey")
#Add vertical grid
abline(v = 1:10, lty = 2, col = "grey")
Another workaround is to use axis
where tck
value is 1
. With axis
, you can specify where the grids should be with at
#Plot
plot(1:10,las=1,xaxp = c(0, 10, 10),xlim=c(0,10), ylim=c(0,10))
#Add horizontal grid
axis(2, at = c(0,2,4,6,8,10), tck = 1, lty = 2, col = "grey", labels = NA)
#Add vertical grid
axis(1, at = 1:10, tck = 1, lty = 2, col = "grey", labels = NA)
#Add box around plot
box()
Related Topics
Is R Superstitious Regarding Posixct Data Type
R: Replace All Values in a Dataframe Lower Than a Threshold with Na
Xgboost in R: How Does Xgb.Cv Pass the Optimal Parameters into Xgb.Train
Writings Functions (Procedures) for Data.Table Objects
How to Convert Ensembl Id to Gene Symbol in R
Loop Over Rows of Dataframe Applying Function with If-Statement
How to Separate Title Page and Table of Content Page from Knitr Rmarkdown PDF
Convert and Save Distance Matrix to a Specific Format
Ggplot2 - Shade Area Above Line
Unnesting a List of Lists in a Data Frame Column
Creating a Sequential List of Letters with R
How to Optimize Read and Write to Subsections of a Matrix in R (Possibly Using Data.Table)
Add Text to Geom_Line in Ggplot
R - How to Test for Character(0) in If Statement