import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
ví dụ 2. Vẽ đường tùy chỉnh phù hợp nhất trong Python
Đoạn mã sau cho biết cách tạo cùng một dòng phù hợp nhất như ví dụ trước ngoại trừ các phần bổ sung sau
- Màu sắc tùy chỉnh cho các điểm và đường phù hợp nhất
- Kiểu dáng và chiều rộng tùy chỉnh cho đường nét phù hợp nhất
- Phương trình của đường hồi quy phù hợp được hiển thị trên biểu đồ
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y, color='purple']
#add line of best fit to plot
plt.plot[x, a*x+b, color='steelblue', linestyle='--', linewidth=2]
#add fitted regression equation to plot
plt.text[1, 17, 'y = ' + '{:.2f}'.format[b] + ' + {:.2f}'.format[a] + 'x', size=14]
Vui lòng đặt phương trình hồi quy phù hợp theo bất kỳ tọa độ [x, y] nào bạn muốn trên biểu đồ
Trong một biểu đồ phân tán được kết nối, hai biến liên tục được vẽ với nhau, với một đường kết nối chúng theo một thứ tự có ý nghĩa, thường là một biến thời gian. Trong biểu đồ bên dưới, chúng tôi chỉ ra "quỹ đạo" của một cặp quốc gia thông qua một không gian được xác định bởi GDP bình quân đầu người và Tuổi thọ trung bình. Tuổi thọ của Botswana
Biểu đồ phân tán có thể được sử dụng với một số nhóm ngữ nghĩa có thể giúp hiểu rõ biểu đồ. Họ có thể vẽ đồ họa hai chiều có thể được nâng cao bằng cách ánh xạ tối đa ba biến bổ sung trong khi sử dụng ngữ nghĩa của các tham số màu sắc, kích thước và kiểu dáng. Và matplotlib rất hiệu quả để tạo các biểu đồ 2D từ dữ liệu trong mảng. Trong bài viết này, chúng ta sẽ xem cách kết nối các điểm biểu đồ phân tán với các đường trong matplotlib
Tiếp cận
- Nhập mô-đun
- Đã xác định tọa độ X và Y cho các điểm biểu đồ phân tán biểu đồ
- Biểu đồ phân tán
- Âm mưu matplotlib. pyplot có cùng tọa độ X và Y
Dưới đây là việc thực hiện
ví dụ 1
Python3
# import module
import
numpy as np
import
matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y, color='purple']
#add line of best fit to plot
plt.plot[x, a*x+b, color='steelblue', linestyle='--', linewidth=2]
#add fitted regression equation to plot
plt.text[1, 17, 'y = ' + '{:.2f}'.format[b] + ' + {:.2f}'.format[a] + 'x', size=14]
0import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y, color='purple']
#add line of best fit to plot
plt.plot[x, a*x+b, color='steelblue', linestyle='--', linewidth=2]
#add fitted regression equation to plot
plt.text[1, 17, 'y = ' + '{:.2f}'.format[b] + ' + {:.2f}'.format[a] + 'x', size=14]
1import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y, color='purple']
#add line of best fit to plot
plt.plot[x, a*x+b, color='steelblue', linestyle='--', linewidth=2]
#add fitted regression equation to plot
plt.text[1, 17, 'y = ' + '{:.2f}'.format[b] + ' + {:.2f}'.format[a] + 'x', size=14]
2 import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y, color='purple']
#add line of best fit to plot
plt.plot[x, a*x+b, color='steelblue', linestyle='--', linewidth=2]
#add fitted regression equation to plot
plt.text[1, 17, 'y = ' + '{:.2f}'.format[b] + ' + {:.2f}'.format[a] + 'x', size=14]
3_______50import
1import
2import
1import
4import
1import
6import
1import
8import
9numpy as np
0
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y, color='purple']
#add line of best fit to plot
plt.plot[x, a*x+b, color='steelblue', linestyle='--', linewidth=2]
#add fitted regression equation to plot
plt.text[1, 17, 'y = ' + '{:.2f}'.format[b] + ' + {:.2f}'.format[a] + 'x', size=14]
2 import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y, color='purple']
#add line of best fit to plot
plt.plot[x, a*x+b, color='steelblue', linestyle='--', linewidth=2]
#add fitted regression equation to plot
plt.text[1, 17, 'y = ' + '{:.2f}'.format[b] + ' + {:.2f}'.format[a] + 'x', size=14]
3numpy as np
3import
1numpy as np
5numpy as np
6import
1numpy as np
8import
1import
0import
1numpy as np
5import
3import
9 Một loại biểu đồ thường được sử dụng khác là biểu đồ phân tán đơn giản, họ hàng gần của biểu đồ đường. Thay vì các điểm được nối với nhau bằng các đoạn thẳng, ở đây các điểm được thể hiện riêng lẻ bằng dấu chấm, hình tròn hoặc hình dạng khác. Chúng tôi sẽ bắt đầu bằng cách thiết lập sổ ghi chép để vẽ sơ đồ và nhập các hàm mà chúng tôi sẽ sử dụng
Trong 1]
%matplotlib inline import matplotlib.pyplot as plt plt.style.use['seaborn-whitegrid'] import numpy as np
Biểu đồ phân tán với x = np.linspace[0, 10, 30]
y = np.sin[x]
plt.plot[x, y, 'o', color='black'];
9
Trong phần trước, chúng ta đã xem xét
x = np.linspace[0, 10, 30] y = np.sin[x] plt.plot[x, y, 'o', color='black'];9/
x = np.linspace[0, 10, 30] y = np.sin[x] plt.plot[x, y, 'o', color='black'];0 để tạo biểu đồ đường. Hóa ra chính chức năng này cũng có thể tạo ra các biểu đồ phân tán