Elena Greg
Elena Greg

Reputation: 1165

How to draw something like simple sun in matplotlib?

How to make a circle and lines outwards, please? Shape like: enter image description here

I have a circle, but I do not know how to continue with lines.

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

fig, ax1 = plt.subplots(figsize=(10,10))

circle = patches.Circle((0.45, 0.5), radius=0.13, transform=ax1.transData, clip_on=False, zorder=10, linewidth=2,
                    edgecolor='black', facecolor=(0, 0, 0, .0125))
ax1.patches.append(circle)
plt.show()

Upvotes: 0

Views: 209

Answers (1)

JohanC
JohanC

Reputation: 80329

Sine and cosine of 16 angles can be used to create the lines:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

fig, ax1 = plt.subplots(figsize=(10, 10))

rad_circ = 0.13
rad_line_start = 0.17
rad_line_end = 0.21
xc, yc = 0.45, 0.5
circle = patches.Circle((xc, yc), radius=rad_circ, transform=ax1.transData, clip_on=False, zorder=10, linewidth=2,
                        edgecolor='black', facecolor='gold')
ax1.patches.append(circle)
theta = np.linspace(0, 2 * np.pi, 16, endpoint=False)
for th in theta:
    ax1.plot([xc + rad_line_start * np.cos(th), xc + rad_line_end * np.cos(th)],
             [yc + rad_line_start * np.sin(th), yc + rad_line_end * np.sin(th)],
             color='gold', lw=10)
ax1.set_aspect('equal')
ax1.axis('off')
plt.show()

example plot

PS: To create all the line segments as a "line collection":

from matplotlib.collections import LineCollection

# ...
line_interval = np.array([[rad_line_start], [rad_line_end]])
segments = np.array([xc + np.cos(theta) * line_interval,
                     yc + np.sin(theta) * line_interval]).T
lc = LineCollection(segments, colors='gold', lw=10)
ax1.add_collection(lc)

Upvotes: 2

Related Questions