Difference between revisions of "Advanced Graphing"

From Scribus Wiki
Jump to: navigation, search
Line 7: Line 7:
 
Like many such projects it began quite manually, making a graph structure by hand, trying out and not liking a grid, and in essence making my own grid to lay out data points. I was getting the data from a spreadsheet, collating manually since there wasn't so much at first, then making a polyline, again manually. This gave me an opportunity to fiddle with various parameters to get the look I wanted.
 
Like many such projects it began quite manually, making a graph structure by hand, trying out and not liking a grid, and in essence making my own grid to lay out data points. I was getting the data from a spreadsheet, collating manually since there wasn't so much at first, then making a polyline, again manually. This gave me an opportunity to fiddle with various parameters to get the look I wanted.
  
The data collection began in mid-April, and continues. As I began updating the graph, I realized I needed some help from the manual drudgery. The first thing I did was to enter the information I wanted into a postgresql database, so I could dissect in various ways, such as by time of day. Later, as the number of data rose, I wanted to be able to look at this by day of week also. This also was helped by having postgresql, where I could for example transform all instances of 23May to M23May, thus making selecting for day of week easier.
+
[[Image:Earlygraph.png|250px]]
 +
 
 +
The data collection began in mid-April, and continues. Quite early, and you can easily see it in this graph from late April, there was this interesting peak at 4pm, so this led if nothing else to continuing the process to see what would happen with larger numbers.
 +
 
 +
As I began updating the graph, I realized I needed some help from the manual drudgery. The first thing I did was to enter the information I wanted into a postgresql database, so I could dissect in various ways, such as by time of day. Later, as the number of data rose, I wanted to be able to look at this by day of week also. This also was helped by having postgresql, where I could for example transform all instances of 23May to M23May, thus making selecting for day of week easier.
  
 
This was still quite manual, since I would export an ordered list to a text file, edit it a bit to making counting how many orders came in per hour, then use Scribus – Properties > Shape > Edit to manually change the nodes of the graph line as needed. Doable but tedious. what especially became annoying was counting lines of a text file, but of course postgresql can use an external script if you write one.
 
This was still quite manual, since I would export an ordered list to a text file, edit it a bit to making counting how many orders came in per hour, then use Scribus – Properties > Shape > Edit to manually change the nodes of the graph line as needed. Doable but tedious. what especially became annoying was counting lines of a text file, but of course postgresql can use an external script if you write one.

Revision as of 23:52, 25 July 2010

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.

What I want to do is explain the workflow that led to the script, and I imagine will continue to suggest improvements. What I wanted to do was to accumulate information about when orders were coming in according to time of day, then see if as data accumulated whether patterns became apparent.

Like many such projects it began quite manually, making a graph structure by hand, trying out and not liking a grid, and in essence making my own grid to lay out data points. I was getting the data from a spreadsheet, collating manually since there wasn't so much at first, then making a polyline, again manually. This gave me an opportunity to fiddle with various parameters to get the look I wanted.

Earlygraph.png

The data collection began in mid-April, and continues. Quite early, and you can easily see it in this graph from late April, there was this interesting peak at 4pm, so this led if nothing else to continuing the process to see what would happen with larger numbers.

As I began updating the graph, I realized I needed some help from the manual drudgery. The first thing I did was to enter the information I wanted into a postgresql database, so I could dissect in various ways, such as by time of day. Later, as the number of data rose, I wanted to be able to look at this by day of week also. This also was helped by having postgresql, where I could for example transform all instances of 23May to M23May, thus making selecting for day of week easier.

This was still quite manual, since I would export an ordered list to a text file, edit it a bit to making counting how many orders came in per hour, then use Scribus – Properties > Shape > Edit to manually change the nodes of the graph line as needed. Doable but tedious. what especially became annoying was counting lines of a text file, but of course postgresql can use an external script if you write one.

#!/usr/bin/env python

# File: axes_graphing.py
# originally 2006.05.17 as graph2.py 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. If you prefer other units, there are commands to get your units type, switch
to points, and then at the end back to your preferred units.
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. The reason for not making this 
automatic is that you might wish to have a family of graphs with a range of values
from graph to graph, yet keep the same scale.

This having been said, nothing will plot unless the script has 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).
If all you wanted to do was to update a graph, then you could add control commands to 
skip making axes, but note the importance of creating the X values, which happens
when the X axis ticks are created.
There is also virtually no error detection, or at least no messages other than
the traceback that will show up.

"""

import scribus

xorigin = 64      # x-origin of graph
yorigin = 536     # y-origin of graph
xaxis=637         # x axis length
yaxis=408         # y axis length
color="Black"
a = 1.5           # width of lines
l = 2.0           # width of the plotted line
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 X 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 Y data unit
    scribus.setRedraw(1)
    scribus.setUnit(0)
    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
        xvalue.append(xtick)
        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(xvalue[item])
        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)
    scribus.setFillColor("None",poly)

scribus.redrawAll()