Cách gắn nhãn nhiều ô trong python

Đô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

Show

đố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 libraries0# importing the libraries1 # importing the libraries2# importing the libraries3# importing the libraries4# importing the libraries5# importing the libraries4# importing the libraries7# importing the libraries8

 

# importing the libraries9

import0# importing the libraries1 # importing the libraries0import3import3# importing the libraries7

 

import6

import7# importing the libraries1 # importing the libraries0import3import3_______3_______2

 

numpy as np3

numpy as np4

numpy as np5# importing the libraries1 numpy as np7# importing the libraries1 numpy as np9import0# importing the libraries4import2import3

import4_______2_______5# importing the libraries8

 

import7

import8

import9# importing the libraries1 matplotlib.pyplot as plt1

matplotlib.pyplot as plt2_______1_______1 matplotlib.pyplot as plt4# importing the libraries8

matplotlib.pyplot as plt6_______1_______1 matplotlib.pyplot as plt8# importing the libraries8

 

# creating data for plot0

# creating data for plot1# creating data for plot2# creating data for plot3# importing the libraries1 # creating data for plot5# importing the libraries8

# creating data for plot7# creating data for plot8# creating data for plot3_______1_______1 matplotlib.pyplot as plt4# importing the libraries8

 

# data arrangement between 0 and 503

# data arrangement between 0 and 504_______7_______5_______6_______3_______1_______1 matplotlib.pyplot as plt8# importing the libraries8

 

# with the difference of 20

# with the difference of 21

 

# with the difference of 22

# with the difference of 23

đầ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 24

# with the difference of 25

# importing the libraries

import numpy as np

import matplotlib.pyplot as plt

 

# creating data for plot

# x-axis2

# x-axis3

# importing the libraries0# importing the libraries1 # importing the libraries2# importing the libraries3# importing the libraries4# importing the libraries5# importing the libraries4# importing the libraries7# importing the libraries8

 

# importing the libraries03

import0# importing the libraries1 # importing the libraries0import3import3# importing the libraries7

 

import6

import7# importing the libraries1 # importing the libraries0import3import3_______3_______2

 

numpy as np3

numpy as np4

numpy as np5# importing the libraries1 numpy as np7# importing the libraries1 numpy as np9import0# importing the libraries4import2import3

import4_______2_______5# importing the libraries8

 

# importing the libraries31

# importing the libraries32

import9# importing the libraries1 matplotlib.pyplot as plt1

# importing the libraries36

# importing the libraries37_______1_______1 matplotlib.pyplot as plt4# importing the libraries8

# importing the libraries41# importing the libraries1 matplotlib.pyplot as plt8# importing the libraries8

 

# creating data for plot0

# creating data for plot1# creating data for plot2# creating data for plot3# importing the libraries1 # creating data for plot5# importing the libraries8

# creating data for plot7# creating data for plot8# creating data for plot3_______1_______1 matplotlib.pyplot as plt4# importing the libraries8

 

# data arrangement between 0 and 503

# data arrangement between 0 and 504_______7_______5_______6_______3_______1_______1 matplotlib.pyplot as plt8# importing the libraries8

 

# with the difference of 20

# with the difference of 21

 

# with the difference of 22

# with the difference of 23

đầ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 libraries69

# importing the libraries70

# importing the libraries

import # importing the libraries73

import matplotlib.pyplot as plt

 

# importing the libraries76

# importing the libraries77# importing the libraries1 # importing the libraries79# importing the libraries80_______1_______81# importing the libraries82# importing the libraries4# importing the libraries84# importing the libraries4

# importing the libraries86# importing the libraries87# importing the libraries4# importing the libraries89# importing the libraries4

# importing the libraries86# importing the libraries92# importing the libraries93

# importing the libraries94

# importing the libraries95# importing the libraries96# importing the libraries97# importing the libraries98# importing the libraries4import00# importing the libraries4import02# importing the libraries4import04# importing the libraries4import06# importing the libraries93

# importing the libraries95import09# importing the libraries97import11# importing the libraries4import13# importing the libraries4import15# importing the libraries4import17# importing the libraries4import11import20

 

import21

import22_______1_______1 import24_______1_______1 import26import27# importing the libraries1 # importing the libraries80# importing the libraries4

import31import32# importing the libraries1 # importing the libraries96_______6_______3# importing the libraries1 import37# importing the libraries4

import31import40# importing the libraries1 numpy as np2_______1_______8

 

import9# importing the libraries1 import24_______1_______1 import26import27# importing the libraries1 # importing the libraries80# importing the libraries4

# importing the libraries95import32# importing the libraries1 import09import57# importing the libraries1 import59# importing the libraries4

# importing the libraries95import62# importing the libraries1 import64import65# importing the libraries1 numpy as np2# importing the libraries4

# importing the libraries95import22# importing the libraries1 import72

 

import73

import4_______2_______75# importing the libraries8

 

import77

# creating data for plot1# importing the libraries80# creating data for plot3# importing the libraries1 matplotlib.pyplot as plt4# importing the libraries8

# creating data for plot7# importing the libraries96# creating data for plot3# importing the libraries1 import88# importing the libraries8

# data arrangement between 0 and 504_______2_______09# creating data for plot3# importing the libraries1 # creating data for plot5# importing the libraries8

 

import96

# with the difference of 21

 

import98

# with the difference of 23

đầ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ó