[ENH]: Could matplotlib be used to draw diagrams (flow charts, mindmap, system architecture, etc) like TikZ for LaTeX? · Issue #30794 · matplotlib/matplotlib · GitHub
Skip to content

[ENH]: Could matplotlib be used to draw diagrams (flow charts, mindmap, system architecture, etc) like TikZ for LaTeX? #30794

Description

@Saltsmart

Problem

We have been looking for a diagram creator, similar to TikZ but more user-friendly in Pythonic style.

Here is an official example for matplotlib: Matplotlib for Making Diagrams, but could the following issues be solved?

  • Draw an element with its absolute size and position, and easily get those attributes so you don't need to compute

  • Auto-resize the canvas without pre-sizing to display all elements

  • "Combine shapes" that allows to manipulate multiple elements for scaling, offsetting, copying... (similar function in PowerPoint or Visio)

Proposed solution

Something like:

from matplotlib import pyplot as plt
from matplotlib import diagrams as mad  # new
from matplotlib.diagrams import Rectangle  # new: inherit from `matplotlib.patches.Rectangle` but with more attributes

fig, cv = mad.canvas(unit="cm")  # something like plt.subplots() but create a auto-resize canvas

rect = Rectangle((0, 0), 1.0, 2.0, figure=fig)  # a 1cm x 2cm rectancle placed at the origin
circ = cv.draw(
    (2, 0),
    shape="circle",
    radius=0.5,
    color="green",
    linestyle="dashed",
    linewidth=2,
)  # another method to draw a circle placed at (2cm, 0cm)

# get the attributes
cv.arrow(
    rect.east,   # will return (1.0, 1.0) like `.east` in TikZ
    circ.north,  # will return (2.0, 0.5) like `.north` in TikZ
)
# if you prefer the style for `matplotlib.axes.Axes.arrow`
# the above method could be overloaded as:
# cv.arrow(
#     x=rect.east.x,
#     y=rect.east.y,
#     dx=circ.north.x - rect.east.x,
#     dy=circ.north.y - rect.east.y,
# )

# "Combine shapes"
group = cv.combine()  # a matplotlib instance or even another canvas!
...
group.place((5, 3), scale=0.5)  # place & scale

fig.savefig("1.pdf", transparent=True, bbox_inches="tight")
fig.show()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions