6
$\begingroup$

I wanna do some plot but I do not know what Mathematica function is best. I wanna draw a 2D plot. Per point in the x Axis I wanna draw ranges, like in the picture I attach enter image description here You might think about adding error plots, but I do not have central values. These are not errors. I just want the ranges. How is this best implemented?

$\endgroup$
10
$\begingroup$
ranges = RandomReal[10, {10, 2}];
ListPlot[Transpose[ranges], 
  Filling -> {1 -> {{2}, Directive[Thick, Red]}}, 
  PlotStyle -> None]

enter image description here

if I want the bars to be located at x=1,5,7,12,20,22,30,39,49,51

To specify horizontal coordinates, you can use

ranges = RandomReal[10, {10, 2}];
xvalues = {1, 5, 7, 12, 20, 22, 30, 39, 49, 51};
ListPlot[Transpose[Thread /@ Transpose[{xvalues, ranges}]], 
 Filling -> {1 -> {{2}, Directive[Thick, Red]}}, PlotStyle -> None]

enter image description here

You can also do it without the Filling option:

ListLinePlot[Thread /@ Transpose[{xvalues, ranges}], 
 PlotStyle -> Directive[Red, Thick]]

same picture

Alternatively, you can use Graphics:

Graphics[{Thick, Red, Line[Thread /@ Transpose[{xvalues, ranges}]]}, 
 Axes -> True, AxesOrigin -> {0, 0}, AspectRatio -> 1 / GoldenRatio]

same picture

$\endgroup$
  • $\begingroup$ kglr , I will like to know which version of Methemetica are you using, I Have 11.1.0 and if copy and paste your solution I have not any plot only axes, If I remove PlotStyle -> None I get the lines with dots blue and red on both extremes of lines. $\endgroup$ – Anxon Pués Jun 25 '18 at 12:10
  • $\begingroup$ @AnxonPués, I am using v9. I updated with a variant the works both in version 11.3 and version 9. $\endgroup$ – kglr Jun 25 '18 at 12:14
  • $\begingroup$ thanks it's ok now, I learn with your solutions $\endgroup$ – Anxon Pués Jun 25 '18 at 12:24
  • $\begingroup$ Thanks! this works. Nonetheless, if I wanna specify the position of the x axis , what is the proper syntax? For instance, In your example, if I want the bars to be located at x=1,5,7,12,20,22,30,39,49,51 ? $\endgroup$ – PhoenixPerson Jun 25 '18 at 12:25
  • $\begingroup$ @PhoenixPerson, please see the update $\endgroup$ – kglr Jun 25 '18 at 13:08
5
$\begingroup$

Here is @kglr's idea but different set up.

ranges1 = RandomReal[10, 10];
ranges2 = RandomReal[10, 10];
ListPlot[{ranges1, ranges2}, 
 Filling -> {1 -> {{2}, Directive[Thick, Red]}}, PlotStyle -> None]

enter image description here

xvalues = {1, 5, 7, 12, 20, 22, 30, 39, 49, 51};
ranges1 = Transpose@{xvalues, RandomReal[10, 10]};
ranges2 = Transpose@{xvalues, RandomReal[10, 10]};
ListPlot[{ranges1, ranges2}, 
 Filling -> {1 -> {{2}, Directive[Thick, Red]}}, PlotStyle -> None]

enter image description here

$\endgroup$
4
$\begingroup$

Take a look at ErrorListPlot and ErrorBar. ErrorListPlot takes the same options as ListPlot so you can customize is as you want.

load the ErrorBar Plotting Package first

Needs["ErrorBarPlots`"]

ErrorListPlot[{{2, 0.4}, {1, 0.4}, {3, 0.4}}, 
 PlotRange -> {{0, 4}, {0, 4}}]

enter image description here

The same result with the different syntax:

ErrorListPlot[{{{1, 2}, ErrorBar[0.4]}, {{2, 1}, 
   ErrorBar[0.4]}, {{3, 3}, ErrorBar[0.4]}}, 
 PlotRange -> {{0, 4}, {0, 4}}]

enter image description here

$\endgroup$
  • 2
    $\begingroup$ ErrorBar is not needed: ErrorListPlot[{{1, 2, 0.4}, {2, 1, 0.4}, {3, 3, 0.4}}, PlotRange -> {{0, 4}, {0, 4}}]. $\endgroup$ – corey979 Jun 25 '18 at 21:35
1
$\begingroup$

Generally, for plots of this type, for readers of the chart that understand statistics, they would prefer you use the BoxWhiskerChart command, to embed some of the statistics of the data in the plots.

Here's the example from the manual page for BoxWhiskerChart:

In[1]:=  data = Table[RandomVariate[NormalDistribution[\[Mu], 1], 100], {\[Mu], {0, 3, 2, 5}}];

In[2]:= BoxWhiskerChart[data]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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