There are quite a few kinds of visual charts, and I'll introduce the common ones in the article. Others are more complicated, and most of them are combined and transformed based on the following types. For beginners, it is easy to be scared by the so many chart types on the official website. Due to too many types, the methods of drawing charts are likely to be confused.

Therefore, here, I have summarized six common basic chart types. And you can learn them through comparison.

1. Line Charts

When drawing a line chart, if you don't have a lot of data, the line of the chart will be zigzag, but once your data set is big -let's say there are 100 points, what we will see with our naked eyes is a smooth curve.

Here I execute plt.plot three times to draw three lines.

import numpy as np import matplotlib.pyplot as plt x= np.linspace(0, 2, 100) plt.plot(x, x, label='linear') plt.plot(x, x**2, label='quadratic') plt.plot(x, x**3, label='cubic') plt.xlabel('x label') plt.ylabel('y label') plt.title("Simple Plot") plt.legend() plt.show()

The image will be:

2. Scatter Plots

In fact, the scatter plot and the line chart are on the same principle; if you connect the points in the scatter plot with lines, you will get a line chart. So you just need to set the line type to draw a scatter plot.

Note: Here I also draw three lines. The difference is that I only use one plt.plot .

import numpy as np import matplotlib.pyplot as plt x = np.arange(0., 5., 0.2) # Red dashes, blue squares, green triangles plt.plot(x, x, 'r--', x, x**2, 'bs', x, x**3, 'g^') plt.show()

The image will be:

3. Histograms

You may say it's so easy, but here I'll draw two histograms for probability in the same image to increase the difficulty. I believe that this kind of histograms you should have been encountered in the actual scenes. Isn't it really convenient to compare them?

import numpy as np import matplotlib.pyplot as plt np.random.seed(19680801) mu1, sigma1 = 100, 15 mu2, sigma2 = 80, 15 x1 = mu1 + sigma1 * np.random.randn(10000) x2 = mu2 + sigma2 * np.random.randn(10000) # the histogram of the data # 50：divide the data into 50 groups # facecolor: color; alpha: transparency # density：it's density rather than specific value n1, bins1, patches1 = plt.hist(x1, 50, density=True, facecolor='g', alpha=1) n2, bins2, patches2 = plt.hist(x2, 50, density=True, facecolor='r', alpha=0.2) # n: probability values; bins: specific values; patches: histogram objects plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(110, .025, r'$\mu=100,\ \sigma=15$') plt.text(50, .025, r'$\mu=80,\ \sigma=15$') # set the specific ranges of x, y axis plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()

The image will be:

4. Bar Charts

Well, I won't draw a simple bar chart here. Let's take look at the two kinds of more difficult bar charts.

4.1 Parallel bar charts

import numpy as np import matplotlib.pyplot as plt size = 5 a = np.random.random(size) b = np.random.random(size) c = np.random.random(size) x = np.arange(size) # just change n for the number of the types total_width, n = 0.8, 3 width = total_width / n # redraw the coordinates of x x = x - (total_width - width) / 2 # here is the offset plt.bar(x, a, width=width, label='a') plt.bar(x + width, b, width=width, label='b') plt.bar(x + 2 * width, c, width=width, label='c') plt.legend() plt.show()

The image will be:

4.2 Stacked bar charts

import numpy as np import matplotlib.pyplot as plt size = 5 a = np.random.random(size) b = np.random.random(size) c = np.random.random(size) x = np.arange(size) # here is the offset plt.bar(x, a, width=0.5, label='a',fc='r') plt.bar(x, b, bottom=a, width=0.5, label='b', fc='g') plt.bar(x, c, bottom=a+b, width=0.5, label='c', fc='b') plt.ylim(0, 2.5) plt.legend() plt.grid(True) plt.show()

The image will be:

5. Pie Charts

5.1 General pie charts

import matplotlib.pyplot as plt labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] # set the distance to separate, and 0 means not to separate explode = (0, 0.1, 0, 0) plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) # Equal aspect ratio ensures that the drawn picture is a perfect circle plt.axis('equal') plt.show()

The image will be:

5.2 Nested pie charts

import numpy as np import matplotlib.pyplot as plt # set the width of each ring size = 0.3 vals = np.array([[60., 32.], [37., 40.], [29., 10.]]) # get the color randomly by get_cmap cmap = plt.get_cmap("tab20c") outer_colors = cmap(np.arange(3)*4) inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10])) print(vals.sum(axis=1)) # [92. 77. 39.] plt.pie(vals.sum(axis=1), radius=1, colors=outer_colors, wedgeprops=dict(width=size, edgecolor='w')) print(vals.flatten()) # [60. 32. 37. 40. 29. 10.] plt.pie(vals.flatten(), radius=1-size, colors=inner_colors, wedgeprops=dict(width=size, edgecolor='w')) # equal makes it a perfect circle plt.axis('equal') plt.show()

The image will be:

5.3 Polar-axis pie charts

The polar-axis pie charts look really very cool, and let's get to know it.

import numpy as np import matplotlib.pyplot as plt np.random.seed(19680801) N = 10 theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) radii = 10 * np.random.rand(N) width = np.pi / 4 * np.random.rand(N) ax = plt.subplot(111, projection='polar') bars = ax.bar(theta, radii, width=width, bottom=0.0) # left indicates where to start # radii (radius) indicates the length drawn from the center point to the edge # width indicates the arc length at the end # custom the color and opacity for r, bar in zip(radii, bars): bar.set_facecolor(plt.cm.viridis(r / 10.)) bar.set_alpha(0.5) plt.show()

The image will be:

6. 3D Charts

6.1 3D scatter plots

import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D data = np.random.randint(0, 255, size=[40, 40, 40]) x, y, z = data[0], data[1], data[2] ax = plt.subplot(111, projection='3d') # create a 3D drawing project # divide the data points into three parts with a distinction in color ax.scatter(x[:10], y[:10], z[:10], c='y') # draw data points ax.scatter(x[10:20], y[10:20], z[10:20], c='r') ax.scatter(x[30:40], y[30:40], z[30:40], c='g') ax.set_zlabel('Z') # axes ax.set_ylabel('Y') ax.set_xlabel('X') plt.show()

The image will be:

6.2 3D planar charts

from matplotlib import pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) X = np.arange(-4, 4, 0.25) Y = np.arange(-4, 4, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) # the concrete function can be viewed with help(function), such as：help(ax.plot_surface) ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow') plt.show()

The image will be: