Scatter And Bars¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | # Plot a scatter plot and histogram of the same data, in two different
# plot frames.
# Data
xs = [
0.83735136, -0.03760732, 0.64764615, -0.56916293, 0.16771351, 0.36879221,
-0.60997142, 0.25521883, 0.11830685, 0.25563545, -0.12038023, -0.37151165,
-0.39852745, 0.04760468, 0.01874401, -0.14411779, -0.47167539, 0.09492484,
-1.0206757 , -0.20679976, 0.64055442, -0.11149991, -0.8195419 , -0.71561298,
0.20467069, 0.04404555, -0.12067043, -0.00652415, 0.1842815 , 1.30424258,
0.18665118, 0.33963749, -0.50445047, 0.23366367, 0.16632812, -0.18822706,
-0.03928118, 0.28480758, -0.77132285, -0.46731356, -0.00351658, 0.57264734,
0.4690202 , 0.39410025, -0.05785553, 0.38895173, -0.60553757, 0.65265759,
0.68512054, 0.82472603, -0.42096647, -0.39180296, 0.13309577, -0.08609454,
0.2812388 , 0.90992544, -0.0474478 , -0.05890777, -0.63456302, -0.2440444 ,
-0.88108586, -0.08369874, 0.2674326 , -0.43100448, -0.56871021, 0.52732416,
-0.69443328, -0.20111549, -0.84669947, -0.16063656, 0.16344137, 0.10416594,
0.03419954, -0.6121973 , 0.57048945, -0.23452936, -0.27359465, 0.24200477,
0.04523835, -0.25387563, -0.66462862, -0.29825646, 0.10609641, -0.02768023,
0.19217027, 0.70849361, 0.01809639, -0.09136528, 0.05425135, -0.47819754,
-0.27667665, 0.02260149, -0.12150936, -0.04109076, 0.33587457, 0.6325594 ,
0.48836006, 0.28791998, -0.30082755, 0.15389378, 0.44696974, 0.42120839,
-0.1975828 , -0.22986685, 0.18926104, -0.89475401, -0.11048363, -0.21441636,
-0.50558513, 0.30749674, -0.15624408, 0.00163378, -0.20476512, 0.28942828,
0.23776839, -0.00519347, 0.18484562, 0.17532173, 0.2595802 , 0.68402716
] * Angstrom
ys = [
-0.39715421, 0.36792564, -0.34670964, 0.00663009, 0.44020473, 0.07102315,
-0.18666689, 0.28657916, 0.12237538, 0.00391008, 0.08724919, -0.5892403,
-0.31992659, -0.05920487, -0.023152 , 0.05400336, 0.71322692, -0.28597838,
0.02357725, -0.2685703 , -0.52583313, 0.44584184, 0.13713652, -0.11021891,
-0.62578892, -0.12033264, -0.25678569, -0.02885627, -0.02127524, -0.42985407,
-0.48594983, -0.54523483, -0.70977806, -0.14108246, -0.01369695, 0.17182939,
0.12188442, -0.00476866, -0.49848981, -0.35582173, 0.50985858, -0.04619683,
0.74132604, 0.92157524, -0.61588049, -0.29860316, -0.61532293, 0.17643255,
0.08809562, 0.07311062, 0.56711457, 0.62286086, -0.14466861, -0.07766289,
-0.52641952, 0.39565981, 0.30934238, -0.35355341, -0.00837116, 0.31705944,
0.16510211, -0.00542853, 0.30364316, 0.00874412, 0.3702261 , -0.17669301,
-0.10746404, -0.10582702, 0.14214246, -0.24628123, 0.45151432, -0.4065585 ,
0.559458 , -0.04214145, 0.25515366, -0.57307117, -0.15778443, 0.34097717,
0.11634075, -0.38137623, -0.68484207, -0.25304297, -0.03233032, 0.20868762,
-0.23503984, 0.57160049, 0.44713183, -0.00655961, 0.54723515, -0.42624796,
0.08212732, -0.25878557, 0.07400322, -0.19167848, -0.4577302 , 0.58497679,
-0.10335792, -0.38983413, 0.1043278 , -0.2605955 , 0.00364975, 0.22308884,
-0.38997689, -0.31123183, -0.50457249, -0.06438019, 0.23835865, 0.03252147,
-0.31160181, 0.0756 , 0.29678598, 0.78384221, -0.63570551, -0.7051451 ,
-0.02914059, 0.10867163, -0.14926761, -0.07602382, 0.57902892, 0.25606282
] * Angstrom
# Create models.
model = Plot.PlotModel(Angstrom, Angstrom)
model.xAxis().setVisible(False)
model.yAxis().setLabel('y')
model_x = Plot.PlotModel(x_unit=Angstrom)
model_x.xAxis().setLabel('x')
model_x.yAxis().setLabel('count')
model_x.yAxis().setAutoscalePadding(0, 0.1)
# Build and add scatter plot
scatter = Plot.Scatter(xs, ys)
scatter.setLabel('Scatter')
model.addItem(scatter)
# Add histogram as for x-values.
x_bins = numpy.linspace(-1.5, 1.5, 24, endpoint=True) * Angstrom
x_counts = numpy.histogram(
xs.inUnitsOf(Angstrom),
x_bins.inUnitsOf(Angstrom)
)[0]
model_x.addItem(Plot.VerticalBar(x_bins, x_counts))
# Change limits.
model.setLimits('x', -2, 2)
model.setLimits('y', -1.5, 1.5)
model_x.setLimits('y')
# Create layout.
layout = Plot.GridLayout(rows=4, columns=1)
layout.setMode(Plot.LAYOUT_MODES.SHARE_X)
# Add main model.
frame = Plot.PlotFrame(use_frame_model=False)
frame.addModel(model)
layout.addItem(frame, row=0, column=0, height=3, width=1)
# Add x model.
layout.addModel(model_x)
# Show the plot for interactive editing.
Plot.show(layout)
# Save the plot (can also be saved to svg, pdf, jpeg or hdf5).
Plot.save(layout, 'scatter_and_bars.png')
|
References: Plot.PlotModel, Plot.VerticalBar, Plot.GridLayout, Plot.PlotFrame, Plot.save, Plot.Scatter, Plot.show.
↩ Go back to Plots