Advanced Graphing

From Scribus Wiki
Revision as of 18:39, 25 July 2010 by Gpittman (talk | contribs)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Some explanatory notes soon. This is an article about a script to create axes with tick marks, then plot data points to the scale of the graph.

#!/usr/bin/env python

# File:
# originally 2006.05.17 as on Scribus wiki
# this version 2010.07.25
# creates graph with axes and plots Y values
# X values are fixed

This script as written assumes a US Letter page in landscape orientation.
The various position and dimension values you see are appropriate for page units
of points.
No headers are created and even the axis labels are not part of this script.
It was written to plot events over a 24 hour day, so the X axis denotes hours
(i.e., 24 tick points), and the Y axis number of events.

The only user input when the script runs is to specify the range of the Y axis,
and indicate the value of individual tick marks. This having been said, nothing
will plot unless you have modified the script and entered values for the yvalue
list. Some arbitrary values are shown below. With a bit more work, you might 
load these values from a file or value dialog(s).


import scribus

xorigin = 64      # x-origin of graph
yorigin = 536     # y-origin of graph
xaxis=637         #x axis length
yaxis=408         #y axis length
a = 1.5 # width of lines
l = 2.0
t = 0.8 # width of scale markers
xvalue = []
yvalue = [1,1,4,2,1,0,1,1,4,1,7,2,3,4,4,2,7,5,5,5,5,4,3,2] # graph points
polyvalue = []
if scribus.haveDoc():
    xtick = xorigin
    xscale = xaxis/24 # this gives you typographic points per data unit
    yrange = int(scribus.valueDialog('Maximum Y Scale', 'Enter Top Y Value\n(Scale)'))
    ymark = int(scribus.valueDialog('Tick Mark Interval', 'Enter Y Interval for Tick Marks'))
    ytick = yorigin
    yscale = yaxis/yrange # this gives you typographic points per data unit
    d = scribus.createLine(xorigin,yorigin,xorigin+xaxis,yorigin) # create X axis
    scribus.setLineWidth(a, d)
    scribus.setLineColor(color, d)
    scribus.setFillColor(color, d)
    e = scribus.createLine(xorigin,yorigin,xorigin,yorigin-yaxis) # create Y axis
    scribus.setLineWidth(a, e)
    scribus.setLineColor(color, e)
    scribus.setFillColor(color, e)
    ytick = ytick - ymark * yscale
    nticks = 0
    item = 0
    while (nticks < 24):
        xtick = xtick + xscale
        xt = scribus.createLine(xtick, yorigin, xtick, yorigin + 5) # creating X axis tick lines
        scribus.setLineWidth(t, xt)
        scribus.setLineColor(color, xt)
        scribus.setFillColor(color, xt)
        nticks += 1
    while (ytick > (yorigin - yaxis)):
        yt = scribus.createLine(xorigin, ytick, xorigin - 5, ytick) # creating Y axis tick lines
        scribus.setLineWidth(t, yt)
        scribus.setLineColor(color, yt)
        scribus.setFillColor(color, yt)
        ytick = ytick - ymark * yscale
    while (item < 24): # this interleaves the X and Y data for createPolyLine
        polyvalue.append((yorigin - yvalue[item] * yscale)) 
        item += 1
    poly = scribus.createPolyLine(polyvalue) # this is the actual plotting
    scribus.setLineWidth(l, poly)
    scribus.setLineColor(color, poly)