包含标签 matplotlib 的文章

Using Matplotlib's fill_between for Area Filling in Python

When plotting with matplotlib in Python, sometimes to enhance the aesthetics of the graph, it’s necessary to fill with color above or below the plot. The ‘fill_between’ function is used for this, where the ‘y2’ parameter determines the second line, indicating whether to fill upwards or downwards.……

阅读全文

Four Plotting Tips in Python

When visualizing data, it’s common to plot multiple charts in a single figure. For example, visualizing the same variable from different perspectives like side-by-side histograms and boxplots for numerical variables is useful.……

阅读全文

Calculating the Gini Coefficient and Plotting the Lorenz Curve with matplotlib

The Gini coefficient and Lorenz curve are widely used to represent data inequality, especially wealth inequality. However, currently in Python, there isn’t a very good function to directly plot the Lorenz curve. Since the current project requires it, this article records how to use numpy, pandas, matplotlib, and other packages to calculate the Gini coefficient and plot the Lorenz curve for practical use.……

阅读全文

Drawing the Butterfly Curve with Python

The butterfly curve, discovered by Temple H. Fay, is a beautiful curve that can be expressed using a polar coordinate function. Because of its elegance, I wanted to use it as my blog’s favicon.ico. Here, I’ll use Python’s matplotlib.pyplot package to draw the desired butterfly curve. First, let’s admire the beautiful butterfly curve.


butter

1. First, We Need to Define the Mathematical Expression of the Butterfly Curve

math

math2

It can also be expressed using spherical coordinates:

math3


2. Choosing matplotlib.pyplot as the Plotting Tool in Python

1. First, import the necessary Python packages

import numpy as np
import matplotlib.pyplot as plt

2. Set the parameter values

t = np.arange(0.0, 12*np.pi, 0.01)
x = np.sin(t)*(np.e**np.cos(t) - 2*np.cos(4*t)-np.sin(t/12)**5)
y = np.cos(t)*(np.e**np.cos(t) - 2*np.cos(4*t)-np.sin(t/12)**5)

3. According to the formula, use numpy functions with plt to draw the required image

plt.figure(figsize=(8,6))
plt.axis('off')
plt.plot(x,y,color='blue',linewidth = '2')
#plt.show()
plt.savefig("butter.jpg",dpi=400)

butter_fly

4. Use Pillow to resize the image to an appropriate size for a favicon

from PIL import Image
im = Image.open("butter.jpg")
favicon = im.resize((50,50))
favicon.save("favicon.ico")

image_ico

……

阅读全文