2

I want to change the background color of the photo because it's not clear I tried to change the Figure of matplotlib background but it doesn't work

the image


    import matplotlib.pyplot as plt
    import ezdxf
    from ezdxf.addons.drawing import RenderContext, Frontend
    from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
    plt.rcParams["savefig.facecolor"] = 'black'
    plt.rcParams['axes.facecolor'] = 'black'

    doc = ezdxf.readfile("second.dxf")
    msp = doc.modelspace()
    doc.layers.new(name='MyLines', dxfattribs={'linetype': 'DASHED', 'color': 8})


    auditor = doc.audit()


    if len(auditor.errors) == 0:
       fig = plt.figure()
       ax = fig.add_axes([0, 0, 1, 1])
       ctx = RenderContext(doc)
       out = MatplotlibBackend(ax)
       Frontend(ctx, out).draw_layout(msp, finalize=True)
       fig.savefig('your.png', dpi=300, facecolor = 'black', edgecolor = 'black')
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52

2 Answers2

2

try overriding MODEL_SPACE_BG_COLOR variable:
ezdxf.addons.drawing.properties.MODEL_SPACE_BG_COLOR = "#FFFFFF"

Mihu
  • 44
  • 2
1

The drawing add-on is still in beta, but you can try this to set the background to white:

if len(auditor.errors) == 0:
    fig = plt.figure()
    ax = fig.add_axes([0, 0, 1, 1])
    ctx = RenderContext(doc)

    # --- add this lines ---
    ctx.set_current_layout(msp)
    ctx.current_layout_properties.set_colors(bg='#FFFFFF')
    # --- add this lines ---

    out = MatplotlibBackend(ax)
    Frontend(ctx, out).draw_layout(msp, finalize=True)
    fig.savefig('your.png', dpi=300, facecolor = 'black', edgecolor = 'black')

mozman
  • 2,001
  • 8
  • 23
  • This has no effect at 0.18.1. I change transparent to True, then I also get a white background. But then the white lines are not visible any longer: fig.savefig('your.png', transparent=True, dpi=300) – TomK Oct 02 '22 at 05:27