In this post, I share the code on how to create a circle and a star chart using the very flexible XY scatter chart type and the very versatile matplotlib Python library. The number of lines of code needed are very small but the key here is to understand the parametric equations needed to generate the shapes and their data points setup in the charts.

The code shown below first draws a circle chart, then before actually plotting it, creates the star chart on the same canvas, then plots them together resulting in a chart with a star within a circle as this:

**Creating the Circle chart**

The formulas for a **circle **with radius (r) and center at (h, k) are:

x = h + r * cos(theta)

y = k + r *sin(theta)

where theta is the angle parameter that varies from 0 to 2pi. In Python, we need the powerful numpy array (theta) containing, say 100 points, evenly spaced between 0 and 2pi using numpy’s linear space function: linspace()

The plt.scatter() function is used to create the scatter plot, and plt.show() to draw the final chart on canvas.

**Creating the 5-point Star chart**

We can also use parametric equations for a star shape using xy scatter plot.

The Parametric equations for a star:

x = radii * np.cos(angles)

y = radii * np.sin(angles)

where angles is array of datapoints that varies from 0 to 2pi (equally spaced).

**We need:**

number of points in the star (e.g., 5 for a typical star)…num_points

outer_radius and inner_radius define the radii of the outer and inner points of the star.

The angles array contains the angles for the points, alternating between the outer and inner points.

The radii array alternates between the outer and inner radii.

The x and y arrays are calculated using the parametric equations for the star.

The plt.scatter() function is used to create the scatter plot, and plt.plot() is used to connect the points with lines.

Because we need the inner and outer points (each with 5 points), we’ll need to generate num_points*2 data points for angles. Again, the values for the angles array will be 0 to 2pi. We generate it using numpy.linspace() for linearly spaced data points array.

To close the star shape lines, we connect the last point to the first (both the x,y coordinates for the last data point) by adding another data point to the x,y numpy array using np.append() function.

To draw just the circle or star just use their respective parametric equations and call plt.show() once for each chart. If you separate the combine code drawing both shapes (as shown below in the widget), be sure to import both the numpy and matplotlib libraries, and call plt.show() for each. Also, add the title for the chart as desired by called plt.title() function for each chart. In the combined code below you’ll see that I have commented out the function calls under circle and star blocks and call it just once for setting the title for the combined chart.

For example, just taking out (or commenting out) the star-specific code, generates the circle as this:

With the same token, taking out (or commenting out) the circle-specific code, generates the star as this:

The complete combined code is shown below. You can run the script by clicking on the button in the widget bleow.

Yes, these can be done in Excel as well. **In the next post, I have shared one of the more advanced and dynamic shaped charts in Excel**…be sure to check it out!

*Did you know about my Patreon page? I’ll be adding more digital downloads there in the shop over time. Hope to see you there!*