Python big data homework six draw a cake

Short answer questions (1 question in total, 100 points) 1. (Short answer questions) Use matplotlib to draw the following figure

The first step is to draw a coordinate system

import matplotlib.pyplot as plt
# Create a Figure artboard object
fig = plt. figure()
# //Divide the entire Figure area into a grid of Row * col;
# //Create the coordinate axis
ax = fig.add_subplot()
# //Set the x-axis and y-axis
ax.set(xlim=[0, 8], ylim=[0, 8], title='HAPPY BIRTHDAY',
       ylabel='Y-Axis', xlabel='X-Axis')
plt. show()

Let’s see the effect

e17fb93d8e7c4bdfad767120747d8584.png

The coordinate system is established

The second step is to draw the grid

import matplotlib.pyplot as plt
# Create a Figure artboard object
fig = plt. figure()
# //Divide the entire Figure area into a grid of Row * col;
# //Create the coordinate axis
ax = fig.add_subplot()
# //Set the x-axis and y-axis
ax.set(xlim=[0, 8], ylim=[0, 8], title='HAPPY BIRTHDAY',
       ylabel='Y-Axis', xlabel='X-Axis')
# show the form
plt.tight_layout()
plt. show()

ad7ba9d0c6124b829eed4a3e115adf2b.png

The third step is to draw the basic graphics

import matplotlib.pyplot as plt
import numpy as np
# Create a Figure artboard object
fig = plt. figure()
# //Divide the entire Figure area into a grid of Row * col;
# //Create the coordinate axis
ax = fig.add_subplot()
# //Set the x-axis and y-axis
ax.set(xlim=[-6, 10], ylim=[-6, 10], title="HAPPY BIRTHDAY",
       ylabel='Y-Axis', xlabel='X-Axis')
#The first parameter (2,1) indicates the starting coordinates, the second parameter 4 is the length, the third parameter is the height 2 alpha is transparency
# rect = plt.Rectangle((2,1),4,2,color='r',alpha=0.3)
# Set legend legend and graphics
x = [-2,-3]
data = [5,5]
#level one
ax.barh(x, width=data, height=1, color="r", label="the first layer of cake", alpha=0.4)
x1 = [-1,0]
data1 = [4,4]
#Second floor
ax.barh(x1,width=data1,left=0.5,height=1,color="b",label="the second layer of cake", alpha=0.4)
#Third Layer Candle
x2 = [1,2,3,4]
y2 = [1.5,1.5,1.5,1.5]
plt.bar(x2, y2, width=0.2, align="center", color="g", label="candle", alpha=0.5, bottom=0.4)
#Leaders of the fourth layer
x2 = [1,2,3,4]
y2 = [2,2,2,2]
plt.bar(x2,y2,width=0.05,align="center",color=['r','b','y','m'],alpha=0.5,bottom=0.4)
# show the table
plt.tight_layout()
plt. legend()
plt. show() 

2c4308b5970d48f0ad7d3f1a65463482.png

alpha is transparency, pit.bar is a histogram, bottom in pit.barw can be adjusted, and it can also be used as a stacked graph

pit.barn is a horizontal bar graph that can be adjusted up and down with the left property

Add a little detail in the fourth step

import matplotlib.pyplot as plt
import numpy as np
# Create a Figure artboard object
fig = plt. figure()
# //Divide the entire Figure area into a grid of Row * col;
# //Create the coordinate axis
ax = fig.add_subplot()
# //Set the x-axis and y-axis
ax.set(xlim=[-6, 10], ylim=[-6, 10], title="HAPPY BIRTHDAY",
       ylabel='Y-Axis', xlabel='X-Axis')
#The first parameter (2,1) indicates the starting coordinates, the second parameter 4 is the length, the third parameter is the height 2 alpha is transparency
# rect = plt.Rectangle((2,1),4,2,color='r',alpha=0.3)
# Set legend legend and graphics
x = [-2,-3]
data = [5,5]
#level one
ax.barh(x, width=data, height=1, color="r", label="the first layer of cake", alpha=0.4)
x1 = [-1,0]
data1 = [4,4]
#Second floor
ax.barh(x1,width=data1,left=0.5,height=1,color="b",label="the second layer of cake", alpha=0.4)
#Third Layer Candle
x2 = [1,2,3,4]
y2 = [1.5,1.5,1.5,1.5]
plt.bar(x2, y2, width=0.2, align="center", color="g", label="candle", alpha=0.5, bottom=0.4)
#Leaders of the fourth layer
x2 = [1,2,3,4]
y2 = [2,2,2,2]
plt.bar(x2,y2,width=0.05,align="center",color=['r','b','y','m'],alpha=0.5,bottom=0.4)
# add a little detail
#1 Add horizontal lines
#x4 (the first abscissa point x41, the second abscissa point x42)
#y4 (y41 corresponding to the first abscissa x41, y42 corresponding to the second abscissa x42)
x4=[0,5]
y4=[-3,-3]
ax.plot(x4, y4)
#Add the second horizontal line
x5=[0,5]
y5=[-2.0,-2.0]
ax.plot(x5, y5, color='b')
#Add the third dotted line
x6=[0.5,4.5]
y6=[-1.0 ,-1.0]
ax.plot(x6, y6, linestyle='--', color='y')
#Add the fourth dotted line
x7=[0.5,4.5]
y7=[0,0]
ax.plot(x7, y7, linestyle='--', color='y')
# show the table
plt.tight_layout()
plt. legend()
plt. show() 

f7dbe174d73a4de4a1ce7556b4ea4320.png

The fifth step is to add background bubbles and remove the abscissa

import matplotlib.pyplot as plt
import numpy as np
# Create a Figure artboard object
fig = plt. figure()
# //Divide the entire Figure area into a grid of Row * col;
# //Create the coordinate axis
ax = fig.add_subplot()
# //Set the x-axis and y-axis
ax.set(xlim=[-6, 10], ylim=[-6, 10], title="HAPPY BIRTHDAY",)
#The first parameter (2,1) indicates the starting coordinates, the second parameter 4 is the length, the third parameter is the height 2 alpha is transparency
# rect = plt.Rectangle((2,1),4,2,color='r',alpha=0.3)
# Set legend legend and graphics
x = [-2,-3]
data = [5,5]
#level one
ax.barh(x, width=data, height=1, color="r", label="the first layer of cake", alpha=0.4)
x1 = [-1,0]
data1 = [4,4]
#Second floor
ax.barh(x1,width=data1,left=0.5,height=1,color="b",label="the second layer of cake", alpha=0.4)
#Third Layer Candle
x2 = [1,2,3,4]
y2 = [1.5,1.5,1.5,1.5]
plt.bar(x2, y2, width=0.2, align="center", color="g", label="candle", alpha=0.5, bottom=0.4)
#Leaders of the fourth layer
x2 = [1,2,3,4]
y2 = [2,2,2,2]
plt.bar(x2,y2,width=0.05,align="center",color=['r','b','y','m'],alpha=0.5,bottom=0.4)
# add a little detail
#1 Add horizontal lines
#x4 (the first abscissa point x41, the second abscissa point x42)
#y4 (y41 corresponding to the first abscissa x41, y42 corresponding to the second abscissa x42)
x4=[0,5]
y4=[-3,-3]
ax.plot(x4, y4)
#Add the second horizontal line
x5=[0,5]
y5=[-2.0,-2.0]
ax.plot(x5, y5, color='b')
#Add the third dotted line
x6=[0.5,4.5]
y6=[-1.0 ,-1.0]
ax.plot(x6, y6, linestyle='--', color='y')
#Add the fourth dotted line
x7=[0.5,4.5]
y7=[0,0]
ax.plot(x7, y7, linestyle='--', color='y')
# draw steam drum background

# Randomly generate xs and ys
n = 1024 # data size
# Randomly select n points from the standard normal distribution curve, the larger the second parameter, the more discrete
xs = np.random.normal(0, 10, n) # Each point is used as the X coordinate of the drawing
ys = np.random.normal(0, 10, n) # Each point is used as the y coordinate of the drawing
# Generate a value according to the coordinate point as the color value
y3 = np.arctan2(ys,xs)
# s: the size of the drawn point
# c: The color of the drawing point, which can be a single color value or a list, and the colors will be sequentially corresponding to the corresponding points
# marker: The style of the point, there are 'o' dots, '+' plus signs, and many more, check the official ~matplotlib.markers.MarkerStyle, the default is 'o'
# linewidth: border width in 'o' dot mode, set to 0, dot has no border
# alpha: Transparency
plt.scatter(xs, ys, s=30, c=y3, alpha=0.5, marker='o', linewidths=0)
# show the table
plt.tight_layout()
plt. legend()
# Replace the label on the coordinate axis with empty (do not display the scale of the coordinate axis)
plt. xticks(())
plt. yticks(())
plt. show() 

eda918de9e8e44a1947e00dc519d8b20.png

Such a delicious cake is ready, don’t hesitate to make one yourself

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledgePython entry skill treeMatplotlib drawing libraryMatplotlib quick start 258461 people are studying systematically