Hướng dẫn how do you label a graph in python? - làm thế nào để bạn gắn nhãn một biể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 you label a graph in python? - làm thế nào để bạn gắn nhãn một biể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:


Đặt kích thước hình và điều chỉnh phần đệm giữa và xung quanh các ô phụ ..

Biểu đồ với nhãn = "line1" bằng phương thức Plot () ..

Biểu đồ với nhãn = "line2" bằng phương thức Plot () ..

  • Để đặt một huyền thoại trên hình, hãy sử dụng phương thức Legend () ..
  • Để hiển thị hình, sử dụng phương thức show () ..
  • Đặt kích thước hình và điều chỉnh phần đệm giữa và xung quanh các ô phụ ..

    Biểu đồ với nhãn = "line1" bằng phương thức Plot () ..

    Biểu đồ với nhãn = "line2" bằng phương thức Plot () ..

    Để đặt một huyền thoại trên hình, hãy sử dụng phương thức Legend () .. Python Matplotlib 

    Để hiển thị hình, sử dụng phương thức show () ..

    Làm thế nào để bạn đặt tên cho một âm mưu trong Python? 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 you label a graph in python? - làm thế nào để bạn gắn nhãn một biể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 you label a graph in python? - làm thế nào để bạn gắn nhãn một biể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 you label a graph in python? - làm thế nào để bạn gắn nhãn một biể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

    Để 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ữ.

    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 you label a graph in python? - làm thế nào để bạn gắn nhãn một biểu đồ trong python?

    Python


    Làm thế nào để bạn đặt tên cho một âm mưu trong Python?

    Để đặt tiêu đề cho cốt truyện trong hàm matplotlib, call title () hàm trên matplotlib.Đối tượng pyplot và vượt qua tên tiêu đề yêu cầu làm đối số cho hàm Tiêu đề ().
    Biểu đồ với nhãn = "line1" bằng phương thức Plot () ..
    Biểu đồ với nhãn = "line2" bằng phương thức Plot () ..
    Để đặt một huyền thoại trên hình, hãy sử dụng phương thức Legend () ..
    Để hiển thị hình, sử dụng phương thức show () ..

    Làm thế nào để bạn đặt tên cho một âm mưu trong Python?

    Để đặt tiêu đề cho cốt truyện trong hàm matplotlib, call title () hàm trên matplotlib.Đối tượng pyplot và vượt qua tên tiêu đề yêu cầu làm đối số cho hàm Tiêu đề ().

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

    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 dán nhãn một điểm duy nhất trong biểu đồ matplotlib trong Python?

    Khoa học dữ liệu thực tế sử dụng Python..
    Khởi tạo danh sách cho X và Y với một giá trị duy nhất ..
    Giới hạn phạm vi trục x và y cho 0 đến 5 ..
    Đặt một lưới theo kiểu dòng hiện tại ..
    Biểu đồ x và y bằng phương thức cốt truyện () với điểm đánh dấu = "o", markeredgecolor = "red", markerfaceColor = "green" ..
    Để hiển thị hình, sử dụng phương thức show () ..