Moving the ticks out of the plot

I've seen other questions around here and on the forum on Mathematica but none of them worked for me. The question is the same: how can I extract the ticks from the plot in order to not overlap to the plot? I've tried lot of the solutions proposed here, but none of them seemed to work for me. Maybe I could have wrongly interpreted the information.

Could somebody help me getting the ticks outside the plot?

DensityPlot[x, {x, 380, 780}, {y, 0, 1},
ColorFunction -> "Rainbow",
AspectRatio -> 1/8,
FrameTicks -> Automatic]

Just to make clear I add a simple plot of a Sine to clarify what i mean by saying that the ticks are "inside the plot", while I want them outside, anyway if you take a look to the previous question you'll find even some solution for the case of a normal plot... Regarding the answer, by scaling the image I get this bad-looking effect...

• @opisthofulax Apologies for my naive answer. I deleted it. Have you tried DensityPlot[x, {x, 380, 780}, {y, 0, 1}, ColorFunction -> "Rainbow", AspectRatio -> 1/8, PlotRangePadding -> None, FrameTicks -> Out]?? I am asking as I cannot see the ticks. Cheers!!! – DiSp0sablE_H3r0 Mar 15 '18 at 23:57
• Out seems not to be a valid options for FrameTicks , ad Mathematica gives me an error if i plug it in... – opisthofulax Mar 15 '18 at 23:58
• The ticks are very small, but inside the picture, if you make whatever plot you want on Mathematica you'll get ticks on the inner part of the frames or the axes – opisthofulax Mar 15 '18 at 23:59

Perhaps you mean something like:

DensityPlot[
x,
{x,380,780},
{y,0,1},
ColorFunction->"Rainbow",
AspectRatio->1/8,
FrameTicks->{
{ChartingScaledTicks[{Identity,Identity}, "TicksLength"->{-.01,-.005}],None},
{ChartingScaledTicks[{Identity,Identity}, "TicksLength"->{-.01,-.005}],None}
}
]

where I used negative tick lengths.

In my original answer, I basically used a negative number for the "positive" tick length. This is a problem because the tick label position only depends on the "negative" tick length. It would have been better to use the documented syntax:

$$\left\{\left\{x_1,\operatorname{label}_1,\left\{\operatorname{plen}_1,\operatorname{mlen}_1\right\}\right\},\ldots \right\}$$

instead, i.e., {0, .01} instead of {-.01, 0}. In fact, using the documented syntax avoids the label/tick collision as long as the AspectRatio is 1. For example, compare:

GraphicsRow[{
Graphics[
{},
PlotRange->{{0,100},{0,10}},
AspectRatio->1,
Frame->True,
FrameTicks->{{None,None},{{{10, 10, {0, .2}}},None}}
],
Graphics[
{},
PlotRange->{{0,100},{0,10}},
Frame->True,
FrameTicks->{{None,None},{{{10, 10, {0, .2}}},None}}
]
}]

Another possible approach to avoid the label/tick collision is to use a plot range that has an aspect ratio of 1:

GraphicsRow[{
Graphics[
{},
PlotRange->{{380, 780}, {0, 400}},
Frame -> True,
FrameTicks -> {{None, None}, {{{400, 400, {0, .2}}}, None}},
AspectRatio -> Full,
ImageSize -> {300, 40}
],
Graphics[
{},
PlotRange->{{380, 780}, {0, 400}},
Frame -> True,
FrameTicks -> {{None, None}, {{{400, 400, {0, .5}}}, None}},
AspectRatio -> Full,
ImageSize -> {300, 40}
]
}]

So, here's a function that leverages ChartingScaledTicks, but adds support for custom tick lengths:

Options[customTicks] = Options[ChartingScaledTicks];

customTicks[args__, opts:OptionsPattern[]][rng__] := Module[{major, minor},
InternalInheritedBlock[{ChartingScaledTicks},
Unprotect[ChartingScaledTicks];
SetOptions[ChartingScaledTicks, "TicksLength"->{1, 2}];
{major, minor} = Replace[
OptionValue[customTicks, "TicksLength"],
Except[{_, _}] :> {{.01, 0.}, {.005, 0.}}
];
major = Replace[major,
{
t_?NumericQ :> {t, 0.},
Except[{_, _}] :> {.01, 0.}
}
];
minor = Replace[minor,
{
t_?NumericQ :> {t, 0.},
Except[{_, _}] :> {.005, 0.}
}
];
Replace[
Charting`ScaledTicks[args, "TicksLength"->Automatic, opts][rng],
{
{a_, b_, {1, 0.}, c___} :> {a, b, major, c},
{a_, b_, {2, 0.}, c___} :> {a, b, minor, c}
},
{1}
]
]
]

Using customTicks for your density plot:

GraphicsRow[{
DensityPlot[
x,
{x,380,780},
{y,0,400},
ColorFunction->"Rainbow",
FrameTicks->{
{None, None},
{customTicks["Linear", "TicksLength"->{{0, .2}, {0, .1}}], None}
},
AspectRatio->Full,
ImageSize->Automatic->{240, 30}
],
DensityPlot[
x,
{x,380,780},
{y,0,400},