Plotting Examples#

Linear and SemiLog plots#

 1import sys
 2import numpy as np
 3import matplotlib.pyplot as plt
 4
 5# If using it with pypi, the following line can be removed
 6sys.path.insert(1, '../src')
 7import chrispytools.plot as cpy
 8
 9# Create sample data
10x = np.linspace(0, 10, 100)
11y1 = np.sin(x)
12y2 = np.cos(x)
13y3 = np.sin(x) * np.cos(x)
14
15
16# Create plot data
17# [ [ X-Data, Y-Data, Label, kwargs for matplotlib ] ]
18plot_data = [
19    [x, y1, "sin(x)"],
20    [x, y2, "cos(x)", 'linestyle=dashed,color=red'],
21    [x, y3, "sin(x)*cos(x)", 'linestyle=:,color=green,marker=o,markersize=2']
22]
23
24plot_data2 = [ [x, y1, "sin(x)"] ]
25
26# Create labels with units
27x_label = ["Time", "s", 1e-3]  # Label, unit, scaling factor (converts to ms)
28y_label = ["Amplitude", "V"]
29
30# Example 1: LinearPlot
31fig = plt.figure(figsize=(3.5, 2.2), dpi=300)
32ax = plt.subplot(111)
33
34cpy.LinearPlot(ax, plot_data, x_label, y_label)
35
36plt.show()
37
38# Example 2: SemiLogXPlot
39fig = plt.figure(figsize=(3.5, 2.2), dpi=300)
40ax = plt.subplot(111)
41
42cpy.SemiLogXPlot(ax, plot_data, x_label, y_label)
43
44plt.show()
45
46# Example 3: SemiLogYPlot
47fig = plt.figure(figsize=(3.5, 2.2), dpi=300)
48ax = plt.subplot(111)
49
50cpy.SemiLogYPlot(ax, plot_data, x_label, y_label)
51
52plt.show()
53
54# Example 4: Complex Plot
55fig = plt.figure(figsize=(3.5, 2.2), dpi=300)
56ax = plt.subplot(111)
57
58cpy.LinearPlot(ax, plot_data2, x_label, y_label)
59
60# Adding a horizontal and vertical lines
61cpy.HLinePlot(ax, 0.75, "750 mV", xDistance=0.5, color="C2", lw=1, fontsize=7)
62cpy.VLinePlot(ax, 2e-3, "2 ms", yDistance=0.25, color="C1", lw=1, fontsize=7)
63
64# Adding a rectangle to highlight some area
65cpy.RectanglePlot(ax, 5e-3, 3e-3, 0, 0.75, color="darksalmon")
66
67plt.show()
68

Histogram and Box plots#

 1import sys
 2import numpy as np
 3import matplotlib.pyplot as plt
 4
 5# If using it with pypi, the following line can be removed
 6sys.path.insert(1, '../src')
 7import chrispytools.plot as cpy
 8
 9data1 = np.random.normal(loc=0, scale=1, size=1000)
10data2 = np.random.normal(loc=0.5, scale=2, size=1000) + 2
11
12
13Y_label = [r'Hits', '']
14X_label = [r'Value', '']
15
16# Example 1: Histogram
17Data_Histo = [ [data1, data1, "Data"] ]
18
19fig = plt.figure(figsize=(3.5, 2.2), dpi=300)
20ax = plt.subplot(111)
21
22cpy.HistogramPlot(ax, Data_Histo, X_label, Y_label, Legend=False)
23
24plt.show()
25
26# Example 2: Histogram
27# Dataset 
28Data_Box = [[[1, 2], [data1, data2], "Data"] ]
29
30# Labels (customize as needed)
31X_label = ["Category", ""]
32Y_label = ["Value", ""]
33
34fig = plt.figure(figsize=(3.5, 2.2), dpi=300)
35ax = plt.subplot(111)
36
37# Use your BoxPlot function
38cpy.BoxPlot(ax, Data_Box, X_label, Y_label)
39
40plt.show()

Special plots#

 1import sys
 2import numpy as np
 3import matplotlib.pyplot as plt
 4
 5# If using from local source during development
 6sys.path.insert(1, '../src')
 7import chrispytools.plot as cpy
 8
 9# Create example grid data
10x = np.linspace(0, 10, 200)
11y = np.linspace(0, 5, 100)
12X, Y = np.meshgrid(x, y)
13
14# 2D function with clear valleys (e.g., interference pattern)
15Z = np.sin(0.5 * X) * np.cos(2 * Y)
16
17# Plot data for PseudoColorPlot
18plot_map = [[X, Y, Z, "2D Function", "cmap=viridis, rasterized=True"]]
19
20# Get Y-position of minimum Z for each X
21y_min = y[ np.argmin(Z, axis=0) ]
22x_min = x
23
24# Compute minima along axis 1 (X-min for each Y)
25x_min2 = x[ np.argmin(Z, axis=1) ]
26y_min2 = y
27
28minima_line = [ [x_min, y_min, "Min along Y", "color=red, linewidth=1"],
29                [x_min2, y_min2, "Min along X", "color=red, linewidth=1, linestyle=--"]
30            ]
31
32
33# Define labels
34x_label = ["X Axis", "", 1]
35y_label = ["Y Axis", "", 1]
36
37# Plotting
38fig = plt.figure(figsize=(5, 3.5), dpi=300)
39ax = plt.subplot(111)
40
41# Pseudocolor background
42cpy.PseudoColorPlot(ax, plot_map, x_label, y_label, Legend=False)
43
44# Overlay minima as a red contour-like line
45cpy.LinearPlot(ax, minima_line, x_label, y_label)
46
47plt.tight_layout()
48plt.show()