|
From: André W. <co...@wo...> - 2020-03-26 09:02:40
|
Hi Frederico,
the methods with _pt at the end, use coordinates in postscript points,
whereas without _pt PyX uses PyX lengths. Those can be configured by the
unit module. PyX lengths default to cm, but you might reconfigure it to
mm to match your expectation.
Please find this correction (to mm) together with a few other small
fixes below. To my mind it now basically runs as you probably expect it.
Best,
André
------
import mpy as np
import math
from pyx import *
unit.set(defaultunit='mm')
def vector(x1, y1, x2, y2, t, pos=0.5, distance=0.1):
c = canvas.canvas()
sz=1
tg_s = (y2-y1) / (x2-x1)
theta = math.atan(tg_s)
dx = sz * math.cos( theta )
dy = sz * math.sin( theta )
#
c.stroke(path.line(x1, y1, x2-dx, y2-dy),
[style.linewidth(sz), color.rgb.black,
deco.earrow([deco.stroked([color.rgb.black])], size=sz)] )
c.text( x2+10, y2+10, t, [text.halign.boxcenter] )
# c.insert(textbox)
return c
rect1 = path.path(path.moveto(0, 0), path.lineto(1, 0),
path.moveto(1, 0), path.lineto(1, 1),
path.moveto(1, 1), path.lineto(0, 1),
path.moveto(0, 1), path.lineto(0, 0))
rect2 = path.path(path.moveto(2, 0), path.lineto(3, 0),
path.lineto(3, 1), path.lineto(2, 1),
path.lineto(2, 0))
rect3 = path.path(path.moveto(4, 0), path.lineto(5, 0),
path.lineto(5, 1), path.lineto(4, 1) )
# path.closepath())
pt=path.path()
c = canvas.canvas()
nlin = 30
mult = 10
lmax = nlin * mult
for ls in range( nlin ) :
# pt = path.path( path.moveto(1,ls) , path.lineto(255,ls) ,
path.closepath() )
pt = path.path(path.moveto(0, ls*mult), path.lineto(lmax,ls*mult),
path.moveto(ls*mult, 0), path.lineto(ls*mult,lmax) )
c.stroke( pt , [style.linewidth.Thin] )
c.text( ls*mult , 0 , str( ls ) )
#for ls in range( nlin ) :
# c.text( ls*mult , 0 , ls , [text.halign.boxcenter ])
for I in range( 1 , 7 ) :
c.insert( vector( 3 * mult , 1 * mult , 5 * mult , I * mult ,
"$\\vec{v}$=(5, " + str(I) + ")" ) )
#vt0 = path.path( path.moveto( vtorigx * mult , vtorigy * mult ) ,
# path.lineto( (vtx + vtorigx) * mult , (vty
+ vtorigy)*mult ) )
#c.stroke( vt0 , [ style.linewidth.THICK , deco.earrow.normal ] )
#c.stroke( pt , [style.linewidth.THICK] )
#c.stroke(rect1, [style.linewidth.THICK])
#c.stroke(rect2, [style.linewidth.THICK])
#c.stroke(rect3, [style.linewidth.THICK])
#c.writeEPSfile("pathitem")
#c.writePDFfile("pathitem")
c.writePDFfile("pathitem")
quit()
------
Am 26.03.20 um 00:22 schrieb Frederico C Wilhelms:
> Howdy,
>
>
> I'm trying PyX for the first time, and looking to adapt examples to my
> need,
>
> but after some effort, couldn't get the text to appear on the canvas.
>
>
> Thought it would write the text somewhere near the drawn vectors. .. but
> it hasn't text anywhere.
>
>
> Would appreciate some help here.
>
>
> Thanks in advance.,
>
> Frederico C Wilhelms
>
>
>
> By the way., Debian , 64 bits , Python Python 3.7.3
> , Pyx 0.15 ., installed by pip3 .,
>
>
> *******
>
>
>
> mpy as np
> import math
> from pyx import *
>
>
>
> def vector(x1, y1, x2, y2, t, pos=0.5, distance=0.1):
> c = canvas.canvas()
> sz=1
> tg_s = (y2-y1) / (x2-x1)
> theta = math.atan(tg_s)
> dx = sz * math.cos( theta )
> dy = sz * math.sin( theta )
> #
> c.stroke(path.line(x1, y1, x2-dx, y2-dy),
> [style.linewidth(sz), color.rgb.black,
> deco.earrow([deco.stroked([color.rgb.black])], size=sz)] )
> c.text_pt( x2+10, y2+10, t, [text.halign.boxcenter] )
> # c.insert(textbox)
> return c
>
> rect1 = path.path(path.moveto(0, 0), path.lineto(1, 0),
> path.moveto(1, 0), path.lineto(1, 1),
> path.moveto(1, 1), path.lineto(0, 1),
> path.moveto(0, 1), path.lineto(0, 0))
> rect2 = path.path(path.moveto(2, 0), path.lineto(3, 0),
> path.lineto(3, 1), path.lineto(2, 1),
> path.lineto(2, 0))
> rect3 = path.path(path.moveto(4, 0), path.lineto(5, 0),
> path.lineto(5, 1), path.lineto(4, 1) )
> # path.closepath())
>
> pt=path.path()
> c = canvas.canvas()
>
> nlin = 30
> mult = 10
> lmax = nlin * mult
> for ls in range( nlin ) :
> # pt = path.path( path.moveto(1,ls) , path.lineto(255,ls) ,
> path.closepath() )
> pt = path.path(path.moveto(0, ls*mult), path.lineto(lmax,ls*mult),
> path.moveto(ls*mult, 0), path.lineto(ls*mult,lmax) )
> c.stroke( pt , [style.linewidth.Thin] )
> c.text( ls*mult , 0 , str( ls ) )
>
>
> #for ls in range( nlin ) :
> # c.text( ls*mult , 0 , ls , [text.halign.boxcenter ])
> for I in range( 1 , 7 ) :
> c.insert( vector( 3 * mult , 1 * mult , 5 * mult , I * mult ,
> repr( "$\vec{v}$=(5, " + str(I) + ")" ) ) )
>
> #vt0 = path.path( path.moveto( vtorigx * mult , vtorigy * mult ) ,
> # path.lineto( (vtx + vtorigx) * mult , (vty
> + vtorigy)*mult ) )
> #c.stroke( vt0 , [ style.linewidth.THICK , deco.earrow.normal ] )
> #c.stroke( pt , [style.linewidth.THICK] )
> #c.stroke(rect1, [style.linewidth.THICK])
> #c.stroke(rect2, [style.linewidth.THICK])
> #c.stroke(rect3, [style.linewidth.THICK])
>
> #c.writeEPSfile("pathitem")
> #c.writePDFfile("pathitem")
>
> c.writePDFfile("pathitem")
> quit()
>
>
>
>
>
>
> _______________________________________________
> PyX-user mailing list
> PyX...@li...
> https://lists.sourceforge.net/lists/listinfo/pyx-user
>
--
by _ _ _
/ \ \ / ) Dr. André Wobst, co...@wo..., https://www.wobsta.de
/ _ \ \/\/ / wobsta GmbH, Amselweg 22, 85716 Unterschleißheim, Germany
(_/ \_)_/\_/ Office: +49 (0)89 5480 3394, Mobile: +49 (0)160 9380 4217
|