Hướng dẫn how do i add a label to a plot in python? - làm cách nào để thêm nhãn vào một âm mưu trong python?

Huyền thoại, tiêu đề và nhãn với matplotlib

Trong hướng dẫn này, chúng tôi sẽ bao gồm các truyền thuyết, tiêu đề và nhãn hiệu trong matplotlib. Rất nhiều lần, đồ thị có thể tự giải thích, nhưng có một tiêu đề cho biểu đồ, nhãn trên trục và một truyền thuyết giải thích những gì mỗi dòng là có thể cần thiết.

Để bắt đầu:

import matplotlib.pyplot as plt

x = [1,2,3]
y = [5,7,4]

x2 = [1,2,3]
y2 = [10,14,12]

Bằng cách này, chúng tôi có hai dòng mà chúng tôi có thể vẽ. Tiếp theo:

plt.plot(x, y, label='First Line')
plt.plot(x2, y2, label='Second Line')

Ở đây, chúng tôi vẽ như chúng tôi đã thấy, chỉ lần này chúng tôi thêm một tham số khác "Nhãn". Điều này cho phép chúng tôi gán một tên cho dòng, mà sau này chúng tôi có thể hiển thị trong truyền thuyết. Phần còn lại của mã của chúng tôi:

plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()

Với plt.xlabel và plt.ylabel, chúng ta có thể gán nhãn cho các trục tương ứng đó. Tiếp theo, chúng ta có thể gán tiêu đề của cốt truyện với plt.title, và sau đó chúng ta có thể gọi huyền thoại mặc định với plt.legend (). Biểu đồ kết quả:

Hướng dẫn how do i add a label to a plot in python? - làm cách nào để thêm nhãn vào một âm mưu trong python?

Có tồn tại 3 bài kiểm tra/câu hỏi cho hướng dẫn này. Để truy cập vào những điều này, tải xuống video và không có quảng cáo.3 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

Hướng dẫn tiếp theo:


pyplot. Hàm văn bản () được sử dụng để thêm văn bản bên trong cốt truyện. Cú pháp thêm văn bản tại một vị trí tùy ý của các trục ..

Nơi x, y - tọa độ ..

  • S - Văn bản được thêm vào bên trong lô (chuỗi).
  • fontdict - tham số tùy chọn. ....
  • pyplot. Hàm văn bản () được sử dụng để thêm văn bản bên trong cốt truyện. Cú pháp thêm văn bản tại một vị trí tùy ý của các trục ..

    Nơi x, y - tọa độ ..

    S - Văn bản được thêm vào bên trong lô (chuỗi). Python Matplotlib 

    fontdict - tham số tùy chọn. ....

    ** kwargs - thuộc tính văn bản .. Let’s create a simple plot

    Python

    import matplotlib

    import matplotlib.pyplot as plt

    import numpy as np

    ____10

    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    2
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    3
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    5
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    7
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    9
    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    0

    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    1

    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    2

    Output:

    Hướng dẫn how do i add a label to a plot in python? - làm cách nào để thêm nhãn vào một âm mưu trong python?

    Lô đất không có nhãn hoặc tiêu đề

    Tạo nhãn cho một cốt truyện

    Bằng cách sử dụng hàm pyplot () của thư viện, chúng tôi có thể thêm XLabel () và ylabel () để đặt nhãn X và Y. & nbsp;

    Ví dụ: Hãy để thêm nhãn trong lô trênLet’s add Label in the above Plot

    Python

    import matplotlib

    import matplotlib.pyplot as plt

    import numpy as np

    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    9
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    2
    plt.title("Survey Of Colony")
    2
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    7
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.title("Survey Of Colony")
    6
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    3
    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    0

    ____10

    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    2
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    3
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    5
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    7
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    9
    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    0

    matplotlib1

    matplotlib2matplotlib3matplotlib4

    matplotlib5matplotlib6matplotlib4

    matplotlib8

    Output:

    Hướng dẫn how do i add a label to a plot in python? - làm cách nào để thêm nhãn vào một âm mưu trong python?

    Lô đất với nhãn

    Nếu bạn muốn làm cho nó dễ hiểu hơn, hãy thêm một tiêu đề vào cốt truyện, chỉ bằng cách thêm một dòng mã duy nhất.Title to the plot, by just adding a single line of code.

    plt.title("Survey Of Colony")

    Example:

    Python3

    import matplotlib

    import matplotlib.pyplot as plt

    import numpy as np

    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    9
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    2
    plt.title("Survey Of Colony")
    2
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    7
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.title("Survey Of Colony")
    6
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    3
    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    0

    ____10

    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    2
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    3
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    5
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    7
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    9
    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    0

    matplotlib1

    matplotlib2matplotlib3matplotlib4

    matplotlib5matplotlib6matplotlib4

    numpy as np4numpy as np5matplotlib4

    matplotlib8

    Output:

    Hướng dẫn how do i add a label to a plot in python? - làm cách nào để thêm nhãn vào một âm mưu trong python?

    Lô đất với tiêu đề

    Đặt thuộc tính phông chữ cho các tiêu đề và nhãn

    Để làm cho cốt truyện hấp dẫn hơn, hãy sử dụng tham số fontdict trong xlabel (), ylabel () và title () để áp dụng các thuộc tính phông chữ.

    Python

    import matplotlib

    import matplotlib.pyplot as plt

    import numpy as np

    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    9
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    2
    plt.title("Survey Of Colony")
    2
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    7
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.title("Survey Of Colony")
    6
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    3
    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    0

    ____10

    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    2
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    3
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    5
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    7
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    4
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    9
    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    0

    Lô đất với tiêu đề

    Đặt thuộc tính phông chữ cho các tiêu đề và nhãn

    matplotlib1

    matplotlib2matplotlib3

    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    59
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    61

    matplotlib5matplotlib6

    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    59
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    61

    numpy as np4numpy as np5

    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    59
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    1
    plt.plot(x, y, label='First Line')
    plt.plot(x2, y2, label='Second Line')
    71

    plt.xlabel('Plot Number')
    plt.ylabel('Important var')
    plt.title('Interesting Graph\nCheck it out')
    plt.legend()
    plt.show()
    2

    Output:

    Hướng dẫn how do i add a label to a plot in python? - làm cách nào để thêm nhãn vào một âm mưu trong python?

    Để làm cho cốt truyện hấp dẫn hơn, hãy sử dụng tham số fontdict trong xlabel (), ylabel () và title () để áp dụng các thuộc tính phông chữ.


    Làm thế nào để bạn dán nhãn một cốt truyện?

    Để dán nhãn đúng biểu đồ, bạn nên xác định biến trục x và trục y nào. Đừng quên bao gồm các đơn vị đo (được gọi là tỷ lệ) để người đọc có thể hiểu từng số lượng được biểu thị bằng các trục đó. Cuối cùng, thêm một tiêu đề vào biểu đồ, thường ở dạng "biến trục y so với biến trục x."identify which variable the x-axis and y-axis each represent. Don't forget to include units of measure (called scale) so readers can understand each quantity represented by those axes. Finally, add a title to the graph, usually in the form "y-axis variable vs. x-axis variable."

    Làm thế nào để bạn hiển thị nhãn trong Python?

    Bằng cách sử dụng hàm pyplot () của thư viện, chúng ta có thể thêm xlabel () và ylabel () để đặt nhãn X và Y..

    Làm thế nào để bạn hiển thị nhãn trên một âm mưu phân tán trong Python?

    Ghi nhãn tất cả các điểm Một số tình huống yêu cầu ghi nhãn tất cả các dữ liệu trong biểu đồ phân tán đặc biệt là khi có một vài điểm dữ liệu.Điều này có thể được thực hiện bằng cách sử dụng một vòng lặp đơn giản để lặp qua bộ dữ liệu và thêm tọa độ X, tọa độ y và chuỗi từ mỗi hàng.using a simple for loop to loop through the data set and add the x-coordinate, y-coordinate and string from each row.

    Làm thế nào để bạn thêm văn bản vào một cốt truyện trong Python?

    pyplot.Hàm văn bản () được sử dụng để thêm văn bản bên trong cốt truyện.Cú pháp thêm văn bản tại một vị trí tùy ý của các trục ...
    Nơi x, y - tọa độ ..
    S - Văn bản được thêm vào bên trong lô (chuỗi).
    fontdict - tham số tùy chọn.....
    ** kwargs - thuộc tính văn bản ..