TransWikia.com

How can I make a list plot with bars?

Mathematica Asked by Uttam Pal on April 29, 2021

Can anyone help me please? I don’t want a Histogram; I don’t want a BarChart, not even a ListPlot with drop down lines. I want a list plot with drop down columns/bars: please see the link.

Mathematica graphics

It is basically a histogram but of a different kind. The data structure contains two variables for example in the above figure it was energy and standard deviation. And the frequency distribution was done based on two cut-offs {0.5,2.0} and finally it was plotted along the energy axis. Therefore, it contains overlapping bars/columns. A projection of a 3D histogram in two dimensions.

2 Answers

Why not just assemble the chart from rectangles?

data = {{-6.65, 55}, {-6.45, 15}, {-6.27, 10}, {-6, 5}, {-5.85, 3}, 
 {-6.46, 6}, {-6.25, 3}, {-6.17, 2}};

Graphics[{EdgeForm[{Thick, Black}], RGBColor[0.3, 0.4, 0.4], 
  Rectangle[{#1 - 0.05, 0}, {#1 + 0.05, #2}] & @@@ data},
 Frame -> True, AspectRatio -> 0.7, FrameLabel -> {"Binding Energy", "Cluster Frequency"}]

enter image description here

Correct answer by Simon Woods on April 29, 2021

BarChart

Although OP says "I don't want a BarChart", the requirements of list plot with drop down columns/bars (overlapping bars/columns) can be achieved using BarChart with a simple ChartElementFunction that produces the desired rectangles for each data point:

ClearAll[ceF]
ceF[w_: .1, df_:"Rectangle"] := ChartElementData[df][{#3[[1]] + {-w, w}/2, #[[2]]}, ##2]&;

Examples:

Using @SimonWoods' example data and options:

data = {{-6.65, 55}, {-6.45, 15}, {-6.27, 10}, {-6, 5}, {-5.85, 3}, 
 {-6.46, 6}, {-6.25, 3}, {-6.17, 2}};

Pre-rocess data to use bin centers as metadata for bin heights:

barchartdata = Rule @@@ (Reverse /@ data);
minmax = Through[{Min, Max}@data[[All, 1]]];

w = .1;
BarChart[barchartdata, ChartElementFunction -> ceFunc[w], 
  ChartBaseStyle -> Opacity[.8], ChartStyle -> "Rainbow",  
  PlotRange -> {minmax + {-w, w}, All}, Axes -> False, Frame -> True, AspectRatio -> 0.7, 
  FrameLabel -> (Style[#, 16, "Panel"] & /@ {"Binding Energy", 
      "Cluster Frequency"})] /. Line[__] :> Sequence[]

enter image description here

Using w = .05:

enter image description here

Using a different built-in ChartElementFunction as input, i.e., with ChartElementFunction -> ceFunc[w, "FadingRectangle"]:

enter image description here

DiscretePlot

Define a simple function using data and plot it using DiscretePlot with your desired setting for the option ExtentSize to control bar widths.

ClearAll[dF]
Table[dF[d[[1]]] = d[[2]], {d, data}];

Examples:

w = .1;
DiscretePlot[dF[i], {i, data[[All, 1]]}, Frame -> True, 
 Axes -> False, PlotRange -> {minmax + {-w, w}, {0, 60}}, 
 ExtentSize -> w, ColorFunction -> "Rainbow", 
 BaseStyle -> EdgeForm[Gray], PlotStyle -> Opacity[0], AspectRatio -> 0.7, 
 FrameLabel -> (Style[#, 16, "Panel"] & /@ {"Binding Energy", "Cluster Frequency"})]

enter image description here

Use w = .05 to get

enter image description here

ListPlot

You can also post-process ListPlot output to change Points into Rectangles.

ClearAll[pntToRect]
pntToRect[w_: .1] := # /. Point -> (Rectangle[{#1 - w/2, 0}, {#1 + w/2, #2}] & @@@ # &) &;

Example:

pntToRect[]@
 ListPlot[List /@ data, PlotStyle -> "Pastel", BaseStyle -> EdgeForm[Gray], 
  PlotRange -> {minmax + {-.1, .1}, {0, 60}}, Frame -> True, 
  Axes -> False, AspectRatio -> 0.7, 
  FrameLabel -> (Style[#, 16, "Panel"] & /@ {"Binding Energy", "Cluster Frequency"})]

enter image description here

Answered by kglr on April 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP