S Hayward
S Hayward

Reputation: 29

What are patches in Python?

I am currently trying to draw a circle in Python. However, the outline of the circle is not plotting. I have tried to change the linestyle, but an error is coming up.

Upvotes: 2

Views: 10125

Answers (2)

tacaswell
tacaswell

Reputation: 87376

The inconsistency in line styles is in the process of being sorted out (https://github.com/matplotlib/matplotlib/pull/3772 ).

A lightning summary of mpl architecture: Figures have 1 or more Axes which have many Artists (subtle detail, Axes and Figure are actually sub-classes of Artist and Figure objects can have other Artists than just Axes). Figure objects also have a Canvas objects (of which there are many implementations for outputting to different formats (ex png, tiff, svg, pdf, eps, ...). When you draw the Figure there is some internal plumbing and each of the Artist objects are recursively drawn to the Canvas.

Most of the plt commands create an Artist and then add it to your current Axes (it pyplot has enough internal state to know what you current Axes is and create one if needed). However, Circle just creates and return a Patch object (which is a type of Artist). It is some what odd that Circle is directly exposed via the pyplot interface.

For this to work you will need to do something like

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

fig, ax = plt.subplots()

# note use Circle directly from patches
circ = mpatches.Circle((1, 0), 5, linestyle='solid', edgecolor='b', facecolor='none')
ax.add_patch(circ)

ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
ax.set_aspect('equal')

Please follow PEP8, you will thank your self later.

Upvotes: 3

Ry-
Ry-

Reputation: 224904

See the list of valid kwargs on the Circle documentationlinestyle can be one of solid, dashed, dashdot, or dotted.

circ = plt.Circle((x,y), R, linestyle='dashed', edgecolor='b', facecolor='none')

Upvotes: 3

Related Questions