Đôi khi, trong khi phân tích bất kỳ dữ liệu nào thông qua biểu đồ, chúng tôi cần hai trục x hoặc y để hiểu thêm về dữ liệu. Thư viện Matplotlib của Python là thư viện trực quan hóa dữ liệu phổ biến nhất và chúng ta có thể tạo bất kỳ loại biểu đồ nào trong Matplotlib. Chúng tôi có thể tạo một biểu đồ có hai trục y và có thể cung cấp các nhãn khác nhau cho cả hai trục y. Chúng ta có thể tạo một biểu đồ với hai trục y khác nhau bằng cách tạo hoặc sử dụng các đối tượng hai trục khác nhau với sự trợ giúp của hàm twinx[]. Đầu tiên, chúng ta tạo các đối tượng hình và trục và tạo biểu đồ đầu tiên. Và chúng tôi cũng đặt nhãn trục x và y với sự trợ giúp của đối tượng trục được tạo
đối tượng trục. Các trục là đơn vị cơ bản và linh hoạt nhất để tạo các ô con. Các trục được sử dụng để sắp xếp các ô tại bất kỳ vị trí nào trong hình. Một hình đã cho của đồ thị có thể chứa nhiều trục, nhưng một đối tượng trục đã cho chỉ có thể nằm trong một hình của đồ thị.
Tiếp cận
- gói nhập khẩu
- Sử dụng đối tượng trục và tạo một ô con
- Sử dụng Twinx[] xác định các giá trị cốt truyện
- Bây giờ dán nhãn trục
- Hiển thị cốt truyện
ví dụ 1
Trong ví dụ này, chúng tôi đã tạo một biểu đồ có hai trục y khác nhau bằng cách sử dụng hai đối tượng trục khác nhau a và a2 với sự trợ giúp của hàm twinx[]. cây rìu. twinx[] tạo đối tượng Axes mới ax2 cho trục y ngược với trục y ban đầu. Đối tượng trục thứ hai ax2 được sử dụng để tạo biểu đồ của biến trục y thứ hai và để cập nhật nhãn của nó
Python3
# Adding a Y-Axis Label to the Secondary Y-Axis in Matplotlib
# importing the libraries
import
numpy as np
import
matplotlib.pyplot as plt
# creating data for plot
# data arrangement between 0 and 50
________số 8_______
# x-axis
# importing the libraries
0# importing the libraries
1 # importing the libraries
2# importing the libraries
3# importing the libraries
4# importing the libraries
5# importing the libraries
4# importing the libraries
7# importing the libraries
8
# importing the libraries
9
import
0# importing the libraries
1 # importing the libraries
0import
3import
3# importing the libraries
7
import
6
import
7# importing the libraries
1 # importing the libraries
0import
3import
3_______3_______2
numpy as np
3
numpy as np
4
numpy as np
5# importing the libraries
1 numpy as np
7# importing the libraries
1 numpy as np
9import
0# importing the libraries
4import
2import
3
import
4_______2_______5# importing the libraries
8
import
7
import
8
import
9# importing the libraries
1 matplotlib.pyplot as plt
1
matplotlib.pyplot as plt
2_______1_______1 matplotlib.pyplot as plt
4# importing the libraries
8
matplotlib.pyplot as plt
6_______1_______1 matplotlib.pyplot as plt
8# importing the libraries
8
# creating data for plot
0
# creating data for plot
1# creating data for plot
2# creating data for plot
3# importing the libraries
1 # creating data for plot
5# importing the libraries
8
# creating data for plot
7# creating data for plot
8# creating data for plot
3_______1_______1 matplotlib.pyplot as plt
4# importing the libraries
8
# data arrangement between 0 and 50
3
# data arrangement between 0 and 50
4_______7_______5_______6_______3_______1_______1 matplotlib.pyplot as plt
8# importing the libraries
8
# with the difference of 2
0
# with the difference of 2
1
# with the difference of 2
2
# with the difference of 2
3
đầu ra
ví dụ 2.
Trong ví dụ này, chúng tôi đã tạo một biểu đồ thanh bằng cùng một phương pháp.
Python3
# with the difference of 2
4
# with the difference of 2
5
# importing the libraries
import
numpy as np
import
matplotlib.pyplot as plt
# creating data for plot
# x-axis
2
# x-axis
3
# importing the libraries
0# importing the libraries
1 # importing the libraries
2# importing the libraries
3# importing the libraries
4# importing the libraries
5# importing the libraries
4# importing the libraries
7# importing the libraries
8
# importing the libraries
03
import
0# importing the libraries
1 # importing the libraries
0import
3import
3# importing the libraries
7
import
6
import
7# importing the libraries
1 # importing the libraries
0import
3import
3_______3_______2
numpy as np
3
numpy as np
4
numpy as np
5# importing the libraries
1 numpy as np
7# importing the libraries
1 numpy as np
9import
0# importing the libraries
4import
2import
3
import
4_______2_______5# importing the libraries
8
# importing the libraries
31
# importing the libraries
32
import
9# importing the libraries
1 matplotlib.pyplot as plt
1
# importing the libraries
36
# importing the libraries
37_______1_______1 matplotlib.pyplot as plt
4# importing the libraries
8
# importing the libraries
41# importing the libraries
1 matplotlib.pyplot as plt
8# importing the libraries
8
# creating data for plot
0
# creating data for plot
1# creating data for plot
2# creating data for plot
3# importing the libraries
1 # creating data for plot
5# importing the libraries
8
# creating data for plot
7# creating data for plot
8# creating data for plot
3_______1_______1 matplotlib.pyplot as plt
4# importing the libraries
8
# data arrangement between 0 and 50
3
# data arrangement between 0 and 50
4_______7_______5_______6_______3_______1_______1 matplotlib.pyplot as plt
8# importing the libraries
8
# with the difference of 2
0
# with the difference of 2
1
# with the difference of 2
2
# with the difference of 2
3
đầu ra
ví dụ 3.
Chúng ta cũng có thể thêm nhãn trục y vào trục y phụ trong pandas. Tạo một biểu đồ từ DataFrame và cũng không sử dụng hàm twinx[]. Trong ví dụ này, chúng tôi sẽ sử dụng DataFrame đơn giản. plot[] với một số tham số để xác định cốt truyện
Khi chúng tôi đặt tham số secondary_y thành True trong DataFrame. plot, nó trả về các trục khác nhau có thể được sử dụng để đặt nhãn
Python3
# importing the libraries
69
# importing the libraries
70
# importing the libraries
import
# importing the libraries
73
import
matplotlib.pyplot as plt
# importing the libraries
76
# importing the libraries
77# importing the libraries
1 # importing the libraries
79# importing the libraries
80_______1_______81# importing the libraries
82# importing the libraries
4# importing the libraries
84# importing the libraries
4
# importing the libraries
86# importing the libraries
87# importing the libraries
4# importing the libraries
89# importing the libraries
4
# importing the libraries
86# importing the libraries
92# importing the libraries
93
# importing the libraries
94
# importing the libraries
95# importing the libraries
96# importing the libraries
97# importing the libraries
98# importing the libraries
4import
00# importing the libraries
4import
02# importing the libraries
4import
04# importing the libraries
4import
06# importing the libraries
93
# importing the libraries
95import
09# importing the libraries
97import
11# importing the libraries
4import
13# importing the libraries
4import
15# importing the libraries
4import
17# importing the libraries
4import
11import
20
import
21
import
22_______1_______1 import
24_______1_______1 import
26import
27# importing the libraries
1 # importing the libraries
80# importing the libraries
4
import
31import
32# importing the libraries
1 # importing the libraries
96_______6_______3# importing the libraries
1 import
37# importing the libraries
4
import
31import
40# importing the libraries
1 numpy as np
2_______1_______8
import
9# importing the libraries
1 import
24_______1_______1 import
26import
27# importing the libraries
1 # importing the libraries
80# importing the libraries
4
# importing the libraries
95import
32# importing the libraries
1 import
09import
57# importing the libraries
1 import
59# importing the libraries
4
# importing the libraries
95import
62# importing the libraries
1 import
64import
65# importing the libraries
1 numpy as np
2# importing the libraries
4
# importing the libraries
95import
22# importing the libraries
1 import
72
import
73
import
4_______2_______75# importing the libraries
8
import
77
# creating data for plot
1# importing the libraries
80# creating data for plot
3# importing the libraries
1 matplotlib.pyplot as plt
4# importing the libraries
8
# creating data for plot
7# importing the libraries
96# creating data for plot
3# importing the libraries
1 import
88# importing the libraries
8
# data arrangement between 0 and 50
4_______2_______09# creating data for plot
3# importing the libraries
1 # creating data for plot
5# importing the libraries
8
import
96
# with the difference of 2
1
import
98
# with the difference of 2
3
đầu ra
Trong ví dụ trên, biểu đồ được tạo mà không sử dụng hàm twinx[], nhưng chúng ta đã tạo đối tượng hai trục ax và ax2 như được đưa ra trong các ví dụ khác cho hai trục y để tạo biểu đồ có hai trục y và cập nhật nhãn của nó