Creating a Graph, Part 2
Jump to navigation
Jump to search
This page will start with the script graph.py, and create a new graph2.py to add some additional features.
graph.py makes a simple graph with X and Y axes, labels for the axes, and a title. With graph2.py we will add the ability to assign a scale to the axes, make user-defined tick marks, and then give some feed back about the typographic features of this graph.
New requestors
#!/usr/bin/env python # File: graph2.py # originally 2006.05.17 # this version 2006.05.18 # creates basic graph with axis labels import scribus xorigin = 150 # x-origin of graph yorigin = 450 # y-origin of graph xaxis=400 #x axis length yaxis=350 #y axis length color="Black" a = 1.5 # width of lines t = 0.8 # width of scale markers if scribus.haveDoc(): title = scribus.valueDialog('Title','Enter Title') xlabel = scribus.valueDialog('X-axis Label','Enter X-Label') ylabel = scribus.valueDialog('Y-axis Label','Enter Y-Label') xrange = int(scribus.valueDialog('Maximum X Scale', 'Enter Top X Value\n(Scale)')) xmark = int(scribus.valueDialog('Tick Mark Interval', 'Enter X Interval for Tick Marks')) xtick = xorigin xscale = xaxis/xrange # this give you data units per axis value in points 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 give you data units per axis value in points scribus.setRedraw(1) scribus.setUnit(0) d = scribus.createLine(xorigin,yorigin,xorigin+xaxis,yorigin) scribus.setLineWidth(a, d) scribus.setLineColor(color, d) scribus.setFillColor(color, d) e = scribus.createLine(xorigin,yorigin,xorigin,yorigin-yaxis) scribus.setLineWidth(a, e) scribus.setLineColor(color, e) scribus.setFillColor(color, e) xtick = xtick + xmark * xscale ytick = ytick - ymark * yscale while (xtick < (xorigin + xaxis)): xt = scribus.createLine(xtick, yorigin, xtick, yorigin - 10) scribus.setLineWidth(t, xt) scribus.setLineColor(color, xt) scribus.setFillColor(color, xt) xtick = xtick + xmark * xscale while (ytick > (yorigin - yaxis)): yt = scribus.createLine(xorigin, ytick, xorigin + 10, ytick) scribus.setLineWidth(t, yt) scribus.setLineColor(color, yt) scribus.setFillColor(color, yt) ytick = ytick - ymark * yscale T = scribus.createText(60, 45, 500, 60) scribus.setTextColor("Red", T) scribus.setText(title, T) scribus.setTextAlignment(1, T) scribus.setFont("Nimbus Roman No9 L Bold", T) scribus.setFontSize(28, T) XL = scribus.createText(xorigin, yorigin + 20, 400, 40) scribus.setText(xlabel, XL) scribus.setTextAlignment(1, XL) scribus.setFont("Luxi Sans Regular", XL) scribus.setFontSize(20, XL) YL = scribus.createText(90, 450, 350, 40) scribus.setText(ylabel, YL) scribus.setTextAlignment(1, YL) scribus.setFont("Luxi Sans Regular", YL) scribus.setFontSize(20, YL) scribus.rotateObject(90,YL) caption = "Scale for this graph\n\n" "X: " + str(xmark) + "units/mark\n" + "Y: " + str(ymark) + "units/mark\n" + str(xmark) + " units = " + str(xscale * xmark) + " pts (X)\n" + str(ymark) + " units = " + str(yscale * ymark) + " pts (Y)" S = scribus.createText(200, 120, 150, 80) scribus.setTextColor("Blue", S) scribus.setText(caption, S) scribus.setTextAlignment(1, S) scribus.setFont("Nimbus Roman No9 L Bold", S) scribus.setFontSize(8, S) scribus.redrawAll()