Tiêu đề Plt Python

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

Đoạn mã sau đặt tiêu đề của cốt truyện thành “Tiêu đề mẫu”

import matplotlib.pyplot as plt plt.title('Sample Title')

Ví dụ

Trong ví dụ này, chúng tôi sẽ vẽ một biểu đồ và đặt tiêu đề của nó thành “Tiêu đề mẫu”

ví dụ. py

import matplotlib.pyplot as plt x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] y = [5, 7, 8, 1, 4, 9, 6, 3, 5, 2, 1, 8] plt.plot(x, y) #set title for plot plt.title('Sample Title') plt.show()

đầu ra

“Tiêu đề mẫu” được đặt làm tiêu đề cho cốt truyện

Phần kết luận

Kết thúc Hướng dẫn Matplotlib này, chúng ta đã học cách đặt tiêu đề cho hình vẽ bằng thư viện Matplotlib trong Python

Có thể kiểm soát vị trí của tiêu đề với tham số loc. Các vị trí khả dụng mà bạn có thể chuyển cho hàm là 'trung tâm', 'trái' và 'phải'. Bạn cũng có thể kiểm soát căn chỉnh bằng cách chuyển các tham số # libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Control position. Available = ‘center’, ‘left’, ‘right’ plt.title("A 2D histogram", loc='left') plt.show() # Then you can adjust with horizontalalignment ('center', 'right', 'left') and # verticalalignment ('top', 'bottom', 'center', 'baseline') plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title("A 2D histogram", loc='left', horizontalalignment='center', verticalalignment='center') plt.show()0 và # libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Control position. Available = ‘center’, ‘left’, ‘right’ plt.title("A 2D histogram", loc='left') plt.show() # Then you can adjust with horizontalalignment ('center', 'right', 'left') and # verticalalignment ('top', 'bottom', 'center', 'baseline') plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title("A 2D histogram", loc='left', horizontalalignment='center', verticalalignment='center') plt.show()1 cho hàm

# libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Control position. Available = ‘center’, ‘left’, ‘right’ plt.title("A 2D histogram", loc='left') plt.show() # Then you can adjust with horizontalalignment ('center', 'right', 'left') and # verticalalignment ('top', 'bottom', 'center', 'baseline') plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title("A 2D histogram", loc='left', horizontalalignment='center', verticalalignment='center') plt.show()

Nét chữ

Các tham số sau có thể được chuyển đến hàm title() để tùy chỉnh phông chữ của tiêu đề

  • # libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Control position. Available = ‘center’, ‘left’, ‘right’ plt.title("A 2D histogram", loc='left') plt.show() # Then you can adjust with horizontalalignment ('center', 'right', 'left') and # verticalalignment ('top', 'bottom', 'center', 'baseline') plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title("A 2D histogram", loc='left', horizontalalignment='center', verticalalignment='center') plt.show()3
  • # libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Control position. Available = ‘center’, ‘left’, ‘right’ plt.title("A 2D histogram", loc='left') plt.show() # Then you can adjust with horizontalalignment ('center', 'right', 'left') and # verticalalignment ('top', 'bottom', 'center', 'baseline') plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title("A 2D histogram", loc='left', horizontalalignment='center', verticalalignment='center') plt.show()4
  • # libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Control position. Available = ‘center’, ‘left’, ‘right’ plt.title("A 2D histogram", loc='left') plt.show() # Then you can adjust with horizontalalignment ('center', 'right', 'left') and # verticalalignment ('top', 'bottom', 'center', 'baseline') plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title("A 2D histogram", loc='left', horizontalalignment='center', verticalalignment='center') plt.show()5
  • # libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Control position. Available = ‘center’, ‘left’, ‘right’ plt.title("A 2D histogram", loc='left') plt.show() # Then you can adjust with horizontalalignment ('center', 'right', 'left') and # verticalalignment ('top', 'bottom', 'center', 'baseline') plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title("A 2D histogram", loc='left', horizontalalignment='center', verticalalignment='center') plt.show()6

# libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Italic and purple, size 20 plt.title("A 2D histogram", fontsize=20, fontweight=0, color='purple', loc='left', style='italic' ) # to write in bold plt.title( "$\mathbf{write in bold}$" , loc="right") # Show the graph plt.show()

nhiều dòng

Bạn có thể dễ dàng thêm một dòng mới vào tiêu đề bằng cách viết \n vào nơi bạn muốn thêm một dòng mới trong văn bản

# libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title('A 2D histogram\nwith the Reds palette', loc='left') # Use plt.subplots_adjust(top=0.7) if you need to save your image. It avoids title to be truncated. # Show the graph plt.show()

nhiều tiêu đề

Có thể thêm nhiều tiêu đề vào biểu đồ

# libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Fist title plt.title("A 2D histogram", loc='left', fontsize=18) # Second title plt.title("made in Python", loc='right', fontsize=13, color='grey', style='italic') # Show the graph plt.show()

Toán học

Bạn có thể viết các biểu thức toán học vào tiêu đề của mình như trong ví dụ này

# libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Add title plt.title("$\mathcal{A}\mathrm{sin}(2 \omega t)$", fontsize=22) # Show the graph plt.show()

phụ đề

Trong ví dụ bên dưới, phụ đề được thêm vào phía trên tiêu đề hình. Vị trí của phụ đề được truyền bởi tham số # libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Control position. Available = ‘center’, ‘left’, ‘right’ plt.title("A 2D histogram", loc='left') plt.show() # Then you can adjust with horizontalalignment ('center', 'right', 'left') and # verticalalignment ('top', 'bottom', 'center', 'baseline') plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title("A 2D histogram", loc='left', horizontalalignment='center', verticalalignment='center') plt.show()7 của hàm # libraries import matplotlib.pyplot as plt import numpy as np # Data x = np.random.normal(size=50000) y = x * 3 + np.random.normal(size=50000) # A histogram 2D plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) # Control position. Available = ‘center’, ‘left’, ‘right’ plt.title("A 2D histogram", loc='left') plt.show() # Then you can adjust with horizontalalignment ('center', 'right', 'left') and # verticalalignment ('top', 'bottom', 'center', 'baseline') plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds) plt.title("A 2D histogram", loc='left', horizontalalignment='center', verticalalignment='center') plt.show()8

Chủ đề