5.4 Graphing

This folder contains examples of plotting and customizing graphs with Python originpro package.

For the graph related functions in originpro, see Graph.

Control Plots As Group

'''
This sample plots a whole worksheet as XY plot and control plots in group
'''
import os
import originpro as op

wks = op.new_sheet()
wks.from_file(os.path.join(op.path('e'), 'Samples', 'Graphing', 'Group.dat'))
graph = op.new_graph(template='scatter')
gl=graph[0]

# plot whole sheet as XY plot
plot = gl.add_plot(f'{wks.lt_range()}!(?,1:end)')

# group the plots and control plots setting in group
gl.group()
plot.colormap = 'Candy'
plot.shapelist = [3, 2, 1]
gl.rescale()

# Customize Legend
lgnd = gl.label('Legend')
lgnd.set_int('fsize', 22)
lgnd.set_int('left',1400)
lgnd.set_int('top',700)
lgnd.set_int('showframe',0)

Graph Legend to Show Only First Plot

'''
This sample shows how to make a multi-layer graph and show legend only on the top layer and to show 
only legend for the first plot, while each layer has 3 plots.
'''
import os
import originpro as op

# Input three data files into three worksheets within one workbook
wb = op.new_book()
wb.set_int('nLayers',3) # Set number of sheets
for wks, fn in zip(wb, ['S15-125-03.dat', 'S21-235-07.dat', 'S32-014-04.dat']):
    wks.from_file(os.path.join(op.path('e'), 'Samples', 'Import and Export', fn))

# Add data plots onto the graph
gp = op.new_graph(template='PAN2VERT')  # load Vertical 2 Panel graph template

# Loop over layers and worksheets to add individual curve.
for i, gl in enumerate(gp):
    for wks in wb:
        plot = gl.add_plot(wks,1+i)
    gl.group()
    gl.rescale()
    
# Customize legend
lgnd = gp[1].label('Legend')

lgnd.set_int('left',4900)
lgnd.set_int('top',100)
#must have something different from internal "\l(1) %(1)" to prevent auto expending
#so I just added a space, but you can add a caption or whatever as long as it is 
#different from the generic internal form
lgnd.text=' \l(1) %(1)'

#we only need one legend, so the one in layer1 will not be needed
gp[0].label('Legend').remove()

Line Plot with Log Scale

'''
This sample creates a sample signal and uses this package to create a periodogram power spectral density plot.
This example requires certain Python packages. To check for and install if needed, 
open the Script Window (Shift+Alt+3), type the following and press Enter:
    pip -chk scipy numpy
'''
import numpy as np
from scipy import signal
import originpro as op

# periodogram power spectral density
fs = 10e3
N = 1e5
amp = 2*np.sqrt(2)
freq = 1234.0
noise_power = 0.001 * fs / 2
time = np.arange(N) / fs
x = amp*np.sin(2*np.pi*freq*time)
x += np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
f, Pxx_den = signal.periodogram(x, fs)

# put the data into worksheet
wks = op.new_sheet(type='w', lname='Periodogram Power Spectral')
wks.from_list(0, f, lname='Freq', units='Hz')
wks.from_list(1, Pxx_den, lname='PSD', units='V\+(2)/Hz')

graph = op.new_graph(template='line')

#log scale
graph[0].yscale = 2   

graph[0].set_xlim(begin=0, end=5000, step=1000)

#step=2 in log
graph[0].set_ylim(1e-7, 1e2, 2) 

graph[0].label('legend').remove()

#page.aa in LT, anti-alias -> On
graph.set_int('aa', 1)

# plot the data into the graph
plot = graph[0].add_plot(wks, coly=1, colx=0, type='line')
plot.color = '#167BB2'

Multiple Layer Plot

'''
This sample shows how to make multi-layer graph with Python 
where data on each layer is from different worksheet. Legend 
is customized to show sheet name.
'''
import os
import originpro as op

# Input three data files into three worksheets within one workbook
wb = op.new_book()
wb.set_int('nLayers',3) # Set number of sheets
for wks, fn in zip(wb, ['S15-125-03.dat', 'S21-235-07.dat', 'S32-014-04.dat']):
    wks.from_file(os.path.join(op.path('e'), 'Samples', 'Import and Export', fn))

# Add data plots onto the graph
gp = op.new_graph(template='PAN2VERT')  # load Vertical 2 Panel graph template

# Loop over layers and worksheets to add individual curve.
for i, gl in enumerate(gp):
    for wks in wb:
        plot = gl.add_plot(wks,1+i)
    gl.group()
    gl.rescale()
    
# Customize legend
lgnd = gp[1].label('Legend')
lgnd.text='\l(1) %(1, @ws)\n\l(2) %(2, @ws)\n\l(3) %(3, @ws)'
lgnd.set_int('left',4900)
lgnd.set_int('top',100)

gp[0].label('Legend').remove()

Plot Contour and 3D Surface Graph

'''
This sample shows how to plot contour and 3D colormap surface from XYZ data. 
The Graph template names and IDs can be found in the page:
https://www.originlab.com/doc/LabTalk/ref/Plot-Type-IDs
'''
import originpro as op

#Import Data File
wks = op.new_sheet()
f = op.path('e')+r'Samples\Matrix Conversion and Gridding\XYZ Random Gaussian.dat'
wks.from_file(f)
wks.cols_axis('xyz') 

# Plot 3D surface
gp = op.new_graph(template='glCMAP')
p = gp[0].add_plot(wks,coly=1,colx=0,colz=2, type=103) 
gp[0].rescale()

# Plot contour
gp = op.new_graph(template='TriContour')
p = gp[0].add_plot(wks,coly=1,colx=0,colz=2, type=243)
p.colormap = 'Maple.pal'
gp[0].rescale()

Plot From Data Range

'''
This sample shows how to use data range to make plot.
'''
import os
import originpro as op

# Import the data file
wks = op.new_sheet()
wks.from_file(os.path.join(op.path('e'), 'Samples', 'Statistics', 'ANOVA', 'One-Way_ANOVA_indexed.dat'))

# Create a graph page
graph = op.new_graph(template='scatter')
gl=graph[0]

# Use the data range string as add_plot() argument.
plot = gl.add_plot(f'{wks.lt_range()}!(1,3)[1:20]')
plot = gl.add_plot(f'{wks.lt_range()}!(1,3)[21:40]')
plot = gl.add_plot(f'{wks.lt_range()}!(1,3)[41:60]')

gl.group()
gl.rescale()

# Customize legend
lgnd = gl.label('Legend')
lgnd.text='\l(1) class 1  \l(2) class 2  \l(3) class 3'
lgnd.set_int('left',1400)
lgnd.set_int('showframe',0)

Plot Heatmap from Matrix Data

'''
This sample shows how to plot a heatmap from matrix data 
It also shows how to set matrix xy map, color scale title and how to set plot colormap
'''
import originpro as op
import numpy as np

arr = np.array([[1, -8], [3, -4], [5, 4], [7, 8]])
mat = op.new_sheet(type='m')
mat.from_np(arr)
#set the 1st matrix's long name, which is the Z lable, which the color scale will use as title
mat.set_label(0, 'Z Levels')
#set the XY mapping in the matrix which will provde the XY scale for the graph
x1,x2,y1,y2 = 0.5, 2.5, 0.5, 4.5
mat.xymap = x1,x2,y1,y2
gr = op.new_graph(template='Heat_Map.otpu')
g = gr[0]
p = g.add_mplot(mat, 0, type = 105)
#heatmap is showing data point at the center, so need to adjust for half step size
hs = 0.5 * (x2-x1)/2;
g.set_ylim(y1-hs, y2+hs, 1)
g.set_xlim(x1-hs, x2+hs, 1)
#set plot colormap
z = p.zlevels
z['minors'] = 15
z['levels'] = [-8, 0, 8]
p.zlevels = z
p.colormap = 'BlueYellow.pal'

Plot Symbols Indexed by Column Values

'''
This sample creates a scatter plot from the A(x) B(y) and control plot symbols from:
col(C) = color index (1=black,2=red etc), offset = 1 from Y which is col(B)
col(D) or col(4), or offset=2 from col(B) = symbol type and so on
modi_col is for genreal modifier control
color_col is specific to color which can further control color types, index, direct RGB, colormap
'''
import originpro as op
wks = op.new_sheet();
wks.cols=6
x=[1,2,3,4,5]
for i in range(6):
    wks.from_list(i,x)

graph = op.new_graph(template='scatter')
layer = graph[0]
plot = layer.add_plot(wks, coly=1, colx=0)
layer.rescale()
plot.color = op.color_col(1, 'n')# the +1 column, n=index
plot.symbol_kind = op.modi_col(2)
plot.symbol_size = op.modi_col(3)
plot.symbol_sizefactor=10
plot.symbol_interior = op.modi_col(4)