0

I calculate with my own written "tree-framework" coordinates for tree-visulize/drawing (therefore I calculate coordinates for the nodes and the connections). I work under python 3.9 and I use for the drawing of the tree the matplotlib (version 3.3.3) and store the "result" with the matplotlib in an file on this way:

ptr_fig.set_canvas(matplotlib.backends.backend_agg.FigureCanvasAgg(ptr_fig)) 
ptr_fig.savefig(temporary_image_file_path, dpi = 100, format = "png")

All this work in general fine and perfect (here an few examples, I have implemented in my framework different tree-drawing-styles): Tree compact drawing style Same tree other drawing style Again same tree again other drawing style

But at huge trees I can have this: Crazzy picture storage

AND THAT IS AN THING WHAT I AT THE MOMENT NOT UNDERSTAND! WHAT GO HERE WRONG? I think it is any wrong setting at the matplotlib, but I don't know what

REMARK: The picture above is in reallity really huge with resolution 56200 * 5200

Another big picture viewed in MS-Edge (therefore picture included in html): Crazzy picture viewed in MS-Edge

In the following an few background information's how I work in PRINCIPLE (the details in the reality are, of course, much more complex ==> I describe only that what is need and simplyfy things, but in principle is all what I write of course true)

With my tree-framework I calculate, for the to visual tree, the coordinates for all the nodes and the connections

  • E.g. min calculated x-coordinate is 0 (e.g. for left side of the most left node in the tree)
  • E.g. max calculated x-coordinate is 100 (e.g. for the right side of the most right node in the tree)

With the max calculated x- and y-coordinate I calculate the figsize (therefore, when the tree is more big so have the figure more inches)

DPI = 100
FACTOR = 10
x_pixels = FACTOR * max_x_tree_coord
y_pixels = FACTOR * max_y_tree_coord
ptr_fig = matplotlib.figure.Figure(figsize = (x_pixels / DPI, y_pixels / DPI),
                                   dpi = DPI)
self.__ptr_axis = ptr_fig.gca()

Before I draw the tree with the matplotlib I "normalize" the cordinates which my tree-framework have calculated so that the x- and y-coord-values, which the matplotlib-framework receive are ONLY between 0 and 1 (therefore e.g. the variables "left" and "bottom" at the code down can have only values between 0 and 1)

Then I draw the nodes and connections, in the following the example how I draw the "box" of an node

p = plt.Rectangle((left, bottom),
                  width,
                  height,
                  fill = fill,
                  edgecolor = edgecolor,
                  facecolor = facecolor,
                  linewidth = linewidth)
p.set_transform(self.__ptr_axis.transAxes)
p.set_clip_on(False)
self.__ptr_axis.add_patch(p)

At the end I do

ptr_fig.set_canvas(matplotlib.backends.backend_agg.FigureCanvasAgg(ptr_fig)) 
ptr_fig.savefig(r"C:\path\to\my\tree_image.png", dpi = 100, format = "png")

I would be very happy when anybody have an idea what go wrong :-)

Already now many thanks for the answers

Best regards Fonso

Fonso
  • 31
  • 3
  • Pngs have a maximum width. It’s not clear why you are creating own canvas. Why do you want such massive figures? 50000 pixels is 10 very big screens wide. – Jody Klymak Dec 21 '21 at 15:01
  • That the pngs have an maximum width was me not clear! Thanks :-)! I create 'own' canvas that the savefig-method 'use' the agg-backend for file-creation. It was until your question really not aware that the file-dimensions are so big (at the moment at which I wrote the post I did the first time view the picture properties and did not deeper think about)! The cause/motivation that I create the picture (which can be so big) like in the post/code described factors is that the image is included in html (and I did set the calculation factors by try and error that the picture appear nice in MS-edge) – Fonso Dec 22 '21 at 10:25

0 Answers0