pyx-project / pyx

Repository of PyX, a Python package for the creation of PostScript, PDF, and SVG files.
https://pyx-project.org/
GNU General Public License v2.0
109 stars 18 forks source link

Change linewidth and linestyle of grid lines #16

Closed sphh closed 4 years ago

sphh commented 4 years ago

Is it possible to change the linewidth and the linestyle of grid lines?

My naïve approach does not work

axispainter = pyx.graph.axis.painter.regular(
    # Make thin and dashed grid lines for major grid
    # No grid lines for sub- and subsubgrid
    gridattrs=[
        pyx.attr.changelist(
            [[pyx.style.linestyle.dashed,
              pyx.style.linewidth.THIn],
             None,
             None])])

Thanks for your help and your great package!!!

wobsta commented 4 years ago

You need to pass the painter to the axis. Also, changeable attributes are meant to replace a single attribute, the term list in changelist in related to the list it selects from, but it still is a single attribute. It is attr.changelist, not attrs.changelist. Fortunately, it is enough to replace one of the attributes in the gridattrs list by None to suppress printing of this tick level grid lines. This means you could also keep the linestyle dashed, but make the linewidth None. Should give the very same result (haven't tested, though).

Here is a full example based on the function example from the PyX website:

from pyx import *

myaxispainter = graph.axis.painter.regular(
    gridattrs=[attr.changelist([style.linestyle.dashed,
                                None]),
               style.linewidth.THIn])

g = graph.graphxy(width=8,
                  x=graph.axis.linear(min=-15,
                                      max=15,
                                      painter=myaxispainter))
g.plot(graph.data.function("y(x)=sin(x)/x"))
g.writePDFfile()

(We know the notation is very verbose. We plan to make setting attributes available to constructors upper in the hirachie, so you could set x_painter_gridattrs in the graph constructor. It is our long term perspective while keeping the internal structure, just making it way more easy to access.)

sphh commented 4 years ago

Aha, I see. Thanks for your help!!