Search code examples
pythonmatplotliblineezdxf

ezdxf and Matplotlib how to increase line thickness


I'm converting a DXF file to a PNG below, but I want to increase the line thickness.

The lines right now are approximately 1 pixel wide right now, I want to be able to make them 5 times as big.

I've tried soloutions from diffrent places like:

mpl.rcParams['lines.linewidth'] = 5  # set the value globally
mpl.rcParams['patch.linewidth'] = 5  # set the value globally
mpl.rcParams['hatch.linewidth'] = 5  # set the value globally
mpl.rcParams['axes.linewidth'] = 5  # set the value globally

A list of other soloutions i've tried:

1. 
fig = plt.figure(linewidth=5)

2.
for ln in ax.lines:
    ln.set_linewidth(5)

3.
for axis in ['top','bottom','left','right']:
    ax.spines[axis].set_linewidth(5)
4. 
[i.set_linewidth(5) for i in ax.spines.itervalues()]

But I got nothing to work properly.

Here's a working example:

main.py

from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
from ezdxf.addons.drawing import Frontend, RenderContext
import ezdxf

import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['lines.linewidth'] = 5  # set the value globally
mpl.rcParams['patch.linewidth'] = 5  # set the value globally
mpl.rcParams['hatch.linewidth'] = 5  # set the value globally
mpl.rcParams['axes.linewidth'] = 5  # set the value globally


dxffilepath = 'GT-001.DXF'
save_to = 'test.png'


def convert_dxf2img(path, save_to, img_format, img_res):
    doc = ezdxf.readfile(path)
    msp = doc.modelspace()
    auditor = doc.audit()

    if len(auditor.errors) != 0:
        return
    else:
        fig = plt.figure()
        ax = fig.add_axes([0, 0, 1, 1])
        ctx = RenderContext(doc)
        ctx.set_current_layout(msp)
        ctx.current_layout.set_colors(bg='#FFFFFF')

        out = MatplotlibBackend(ax)
        Frontend(ctx, out).draw_layout(msp, finalize=True)

        fig.savefig(save_to, dpi=img_res)
        plt.close(fig)

convert_dxf2img(dxffilepath, save_to, img_format='.png', img_res=300)

The lines are to thin, I want to make them 5 times as thick. Is there any way to do that?

Output I get: output


Solution

  • An extended control of the backend will be possible in the next version of ezdxf v0.15, stable alpha versions already available at PyPI.

    Scale the lineweight by a factor of 5:

        ...
        out = MatplotlibBackend(ax, params={"lineweight_scaling": 5})
        ...
    
    

    More backend options are documented here and the params argument is also supported by the qsave() function.