0
$\begingroup$

When I try putting dot graph(ListPlot) named dg as below to my Histogram graphic.The horizental axis is not exactly aligned. How to fix my code?

dg = ListPlot[{Transpose[{{0.66, -0.09}, ConstantArray[0.1, i]}]}, 
  PlotStyle -> Red, PlotRange -> {{-6, 6}, {0, 5}}, 
  PlotRangePadding -> {{0, 0}, {0, 0}}]

enter image description here

(*How to fix code?*)    
Histogram[{0.66, -0.09}, PlotRange -> {{-6, 6}, Automatic},
     Epilog -> {Inset[dg, Scaled@{0, 0}, Scaled@{0, 0}, Scaled[1]]},
     PlotRangePadding -> {{0, 0}, {0, 0}}]

enter image description here

$\endgroup$
1
  • 3
    $\begingroup$ Use Show to combine the two plots, rather than an Inset. Show[{Histogram[...], dg}] after removing the Epilog from Histrogram. $\endgroup$
    – MarcoB
    Nov 15, 2023 at 15:56

1 Answer 1

3
$\begingroup$

1. Using Epilog + Inset

  1. Use the same ImagePadding (say ImagePadding -> 20) in ListPlot and Histogram.
  2. Use ImageScaled to specify size (the fourth argument) in Inset[...].

dg = ListPlot[{Transpose[{{0.66, -0.09}, ConstantArray[0.1, 2]}]}, 
   PlotStyle -> Red, 
   PlotRange -> {{-6, 6}, {0, 5}}, 
   PlotRangePadding -> 0, 
   ImagePadding -> 20];

Histogram[{0.66, -0.09}, 
 PlotRange -> {{-6, 6}, Automatic}, 
 Epilog -> {Inset[dg, {0, 0}, {0, 0}, ImageScaled[1]]}, 
 PlotRangePadding -> 0, 
 ImagePadding -> 20]

enter image description here

In this case, Inset[dg, Scaled@{0, 0}, Scaled@{0, 0}, ImageScaled[1]] gives the same result.

Add the option AxesOrigin -> {3,0} in Histogram[...] to show two vertical axes:

enter image description here

See:

2. Epilog only

An easier approach is to avoid the alignment issue using graphics primitives from dg (that, is dg[[1]]) as the option value for Epilog and, if needed, modify the vertical tick labels using Charting`FindTicks.

Histogram[{0.66, -0.09}, 
 PlotRange -> {{-6, 6}, Automatic}, 
 Epilog -> {dg[[1]]}, 
 PlotRangePadding -> 0
 Ticks -> {Automatic, Charting`FindTicks[{0, 1}, {0, 5}]}, 
 AxesOrigin -> {0, 0}]

enter image description here

3. Show

Combine dg and histogram using Show as suggested by MarcoB in comments; and, if needed, modify the vertical tick labels:

Show[Histogram[{0.66, -0.09}, PlotRange -> {{-6, 6}, Automatic}, 
  PlotRangePadding -> 0], dg, 
 Ticks -> {Automatic, Charting`FindTicks[{0, 1}, {0, 5}]}, 
 AxesOrigin -> {0, 0}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.