Cách vẽ đường viền trong python

Các ô đường viền có thể được tạo bằng Matplotlib. Trong Kỹ thuật Xây dựng, một biểu đồ đường viền có thể hiển thị cấu trúc liên kết của một tòa nhà. Trong Kỹ thuật cơ khí, một biểu đồ đường viền có thể hiển thị độ dốc ứng suất trên bề mặt một phần

Phương pháp
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

9 của Matplotlib

Xây dựng các biểu đồ đường viền với Matplotlib đòi hỏi phải sử dụng phương pháp

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

0. Cuộc gọi phương thức
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

0 cơ bản bên dưới

ax.contour[X, Y, Z]

Trong đó

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

1 và
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

2 là mảng 2D của các điểm x và y và Z là mảng 2D gồm các điểm xác định "chiều cao" của đường bao, được biểu thị bằng màu trong biểu đồ 2D

Đoạn mã bên dưới bao gồm hàm

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

3 của NumPy tạo hai mảng 2D từ hai mảng 1D

Trong 1]

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

Phương pháp
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

9 của Matplotlib

Phương pháp Matplotlib _______ 15 tương tự như ________ 30 ngoại trừ việc ________ 15 tạo ra các biểu đồ đường viền được "điền". Thay vì các dòng trong một ô

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

0, các khu vực bóng mờ được tạo ra bởi một ô
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

5. Cuộc gọi phương thức chung cho
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

5 tương tự như
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

0

ax.contourf[X, Y, Z]

Trong đó

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

1 và
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

2 là mảng 2D của các điểm x và y và Z là mảng 2D gồm các điểm xác định màu của các vùng trên biểu đồ 2D

Phần mã tiếp theo xây dựng biểu đồ đường viền bóng mờ bằng cách sử dụng phương pháp

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

5 của Matplotlib

Trong 2]

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

3

Thanh màu trên ô đường viền

Vì màu sắc đại diện cho chiều thứ ba [như "cao"] trên biểu đồ 2D nên sẽ hữu ích khi có tỷ lệ cho ý nghĩa của từng màu. Một thang màu thường được biểu diễn bên cạnh một ô

Các thanh màu được thêm vào các ô đường viền Matplotlib bằng phương pháp

ax.contourf[X, Y, Z]
5. Vì thanh màu không phải là một phần của biểu đồ đường bao nên thanh màu cần được áp dụng cho đối tượng hình, thường được gọi là
ax.contourf[X, Y, Z]
6. Một biểu đồ đường viền cần được chuyển vào phương thức
ax.contourf[X, Y, Z]
5. Do đó, khi bạn thêm một thanh màu vào một hình, một đối tượng vẽ cần phải có sẵn. Một đối tượng cốt truyện là đầu ra của phương thức
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

5

Trước đây, đầu ra của phương thức

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

5 không được gán cho một biến. Nhưng để bao gồm một thanh màu trên biểu đồ đường viền, đối tượng biểu đồ cần được lưu vào một biến, để đối tượng biểu đồ có thể được truyền cho phương thức
ax.contourf[X, Y, Z]
5

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

0

Trong đó

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

31 là đối tượng cốt truyện được tạo bởi
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

32. Đối tượng axis chứa biểu đồ đường bao,
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

33 được truyền cho phương thức
ax.contourf[X, Y, Z]
5 cùng với đối tượng biểu đồ
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

31

Đoạn mã bên dưới tạo một biểu đồ đường viền được tô với một thanh màu được đặt bên cạnh biểu đồ

Trong 3]

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

6

Bản đồ màu trên các ô đường viền

Bảng màu mặc định của đường viền Matplotlib và các ô đường viền đã điền có thể được sửa đổi. Một cách chung để sửa đổi lược đồ màu là gọi hàm

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

36 của Matplotlib để xuất ra một đối tượng bản đồ màu. Có nhiều bản đồ màu khác nhau có sẵn để áp dụng cho các ô đường viền. Một danh sách đầy đủ có sẵn trong tài liệu Matplotlib [https. // matplotlib. tổ chức]. Đối tượng colormap sau đó được chuyển đến phương thức
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

5 hoặc
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = np.arange[-3.0, 3.0, 0.1] y = np.arange[-3.0, 3.0, 0.1] X, Y = np.meshgrid[x, y]

Z = np.sin[X]*np.cos[Y]

fig, ax = plt.subplots[figsize=[6,6]]

ax.contour[X,Y,Z]

plt.show[]

0 dưới dạng đối số từ khóa

Chủ Đề