Hướng dẫn how do you plot a diagonal line in python? - làm thế nào để bạn vẽ một đường chéo trong python?

Vẽ một đường chéo từ phía dưới bên trái đến các góc trên bên phải của cốt truyện của bạn sẽ được thực hiện bằng cách

import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
5

Sử dụng

import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
6, tọa độ
import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
7 và
import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
8 được cung cấp được hiểu là tọa độ trục thay vì tọa độ dữ liệu.

Điều này, như @FQQ chỉ ra, chỉ là dòng nhận dạng khi giới hạn

import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
7 và
import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
8 của bạn bằng nhau. Để vẽ dòng
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 13, 100)
y = x**2

plt.plot(x, y)
1 sao cho nó luôn mở rộng đến giới hạn của cốt truyện của bạn, một cách tiếp cận tương tự như phương pháp được đưa ra bởi @ffisegydd sẽ hoạt động và có thể được viết dưới dạng hàm sau.

def add_identity(axes, *line_args, **line_kwargs):
    identity, = axes.plot([], [], *line_args, **line_kwargs)
    def callback(axes):
        low_x, high_x = axes.get_xlim()
        low_y, high_y = axes.get_ylim()
        low = max(low_x, low_y)
        high = min(high_x, high_y)
        identity.set_data([low, high], [low, high])
    callback(axes)
    axes.callbacks.connect('xlim_changed', callback)
    axes.callbacks.connect('ylim_changed', callback)
    return axes

Ví dụ sử dụng:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()

Đường cong điểm-điểm

Để tạo một âm mưu đơn giản kết nối các điểm trong một mặt phẳng Cartesian:

  • Nhập mô -đun pyplot từ thư viện matplotlib và đặt cho nó tên tốc ký pltpyplot module from the Matplotlib library and give it the shorthand name plt
  • Tạo một lô với hàm
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    2

Ở đây, một ví dụ, hiển thị parabola (\ (y = x^2 \)):\(y = x^2\)):

import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)

Đường cong mịn

Khi bạn chỉ vẽ một vài điểm như trong ví dụ trước, đường cong kết quả sẽ trông lởm chởm. Nếu bạn đang vẽ một chức năng liên tục như thế này, nó thường tốt hơn để có một đường cong trơn tru. Mặc dù về mặt kỹ thuật, có thể vẽ một đường cong liên tục bằng phương pháp này, nhưng ít nhất bạn có thể làm cho dòng có vẻ mượt mà bằng cách tăng số lượng điểm bạn vẽ:smooth curve. While it isn’t technically possible to plot a continuous curve using this method, you can at least make the line appear smooth by simply increasing the number of points you plot:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 13, 100)
y = x**2

plt.plot(x, y)

Ví dụ trên là vẽ 100 điểm và kết nối chúng với các đường thẳng. Vì vậy, các cạnh lởm chởm vẫn tồn tại nhưng bây giờ chúng quá nhỏ để được chú ý! Lưu ý chức năng được sử dụng để tạo X-DATA: Numpy từ

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 13, 100)
y = x**2

plt.plot(x, y)
3. Hàm này tạo ra một mảng các số cách đều nhau giữa một điểm bắt đầu nhất định và một điểm kết thúc nhất định với số lượng giá trị được tạo là đầu vào thứ ba cho hàm. Trong trường hợp này, các đầu vào là 0, 13 và 100, do đó, 100 giá trị được tạo bắt đầu từ 0 và kết thúc ở 13. Data Y sau đó được tạo bằng cách lấy hình vuông của dữ liệu X.

Đường thẳng đứng và ngang

Nếu bạn muốn vẽ một đường thẳng, bạn có thể chỉ cần vẽ hai điểm và chúng sẽ được kết nối như mong đợi. Trong các trường hợp đặc biệt mà bạn muốn tạo các đường thẳng đứng hoặc dọc mở rộng bên phải đến đầu của biểu đồ theo cả hai hướng, có các hàm đặc biệt

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 13, 100)
y = x**2

plt.plot(x, y)
4 và
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 13, 100)
y = x**2

plt.plot(x, y)
5:

# Plot a straight diagonal line
plt.plot([0, 9], [0, 8])
# Plot a horizontal line
plt.axhline(4)
# Plot a vertical line
plt.axvline(7)

Tùy chọn định dạng

Thay đổi giao diện của cốt truyện với các tùy chọn sau:

  • Đặt tiêu đề với
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    6title with
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    6
  • Đặt nhãn trục với
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    7 và
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    8axis labels with
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    7 and
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    8
  • Thay đổi giới hạn trục bằng
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    9 và
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    0axis limits with
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    9 and
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    0
  • Thay đổi màu và chiều rộng dòng bằng cách sử dụng các đối số từ khóa
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    1 và
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    2 trong cuộc gọi
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    2 (xem trang này để biết tất cả các tùy chọn màu và dòng)line colour and width using the
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    1 and
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    2 keyword arguments in the
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    2 call (see this page for all the colour and line options)
  • Cuối cùng, hiển thị cốt truyện với
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    4display the plot with
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    4

Ở đây, một ví dụ sử dụng điểm nhật ký của Manchester City sau mỗi trận đấu của mùa giải Premier League Anh 2018-19:

import matplotlib.pyplot as plt

week = [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
    21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
]
log_points = [
    3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
    47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
]

plt.plot(week, log_points, c='#6caddf', lw=3)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)

plt.show()

Lựa chọn khác

Một số tùy chọn khác có thể được sửa đổi với:

  • Tính minh bạch của dòng: Sử dụng đối số từ khóa
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    5 trong
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    2
    : use the
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    5 keyword argument within
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    2
  • Các đường lưới: Sử dụng hàm
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    7 trong đó bạn có thể đặt
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    8 đường lưới thành đánh dấu (chính, phụ hoặc cả hai) và
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    9 để áp dụng các dòng cho (x, y hoặc cả hai), cùng với các đối số từ khóa khác liên quan đến các lô dòng
    : use the
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    7 function in which you can set
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    8 gridlines to mark (major, minor or both) and the
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    9 to apply the lines to (x, y or both), along with other keyword arguments related to line plots
    • Nếu bạn muốn các đường lưới nhỏ và đánh dấu trục, bạn cũng sẽ cần sử dụng
      import matplotlib.pyplot as plt
      
      week = [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
      ]
      log_points = [
          3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
          47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
      ]
      
      plt.plot(week, log_points, c='#6caddf', lw=3)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      
      plt.show()
      0
plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.grid(which='major', c='grey', lw='0.5', linestyle='-')

plt.show()

Văn bản và chú thích

Có một sự khác biệt giữa văn bản và chú thích trên biểu đồ:text and annotations on a graph:

  • Nhãn văn bản chỉ là văn bản. Chúng được thêm vào với chức năng
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    1 cho phép bạn chỉ định các tọa độ X và Y của vị trí nhãn Lốc cùng với chuỗi sẽ xuất hiện. Ngoài ra còn có các tùy chọn bổ sung có thể được chỉnh sửa, chẳng hạn như
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    2 (liên kết ngang của văn bản),
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    3 và
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    4
    are text only. They are added with the
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    1 function which allows you to specify the x- and y-coordinates of the label’s position along with the string that should appear. There are also additional options that can be edited, such as
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    2 (the horizontal alignment of the text),
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    3 and
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    4
  • Chú thích là văn bản với mũi tên. Chúng có thể được thêm vào với
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    5 cùng với một số đối số. Các đối số này có thể được chỉ định bằng cách sử dụng từ khóa của chúng hoặc chúng có thể được đưa ra mà không cần từ khóa nếu chúng theo thứ tự sau:
    are text with arrows. These can be added with
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    5 together with a number of arguments. These arguments can be specified by using their keywords or they can be given without keywords if they are in the following order:
    • import matplotlib.pyplot as plt
      
      week = [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
      ]
      log_points = [
          3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
          47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
      ]
      
      plt.plot(week, log_points, c='#6caddf', lw=3)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      
      plt.show()
      6 là chuỗi sẽ xuất hiện
    • import matplotlib.pyplot as plt
      
      week = [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
      ]
      log_points = [
          3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
          47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
      ]
      
      plt.plot(week, log_points, c='#6caddf', lw=3)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      
      plt.show()
      7 là một tuple chứa tọa độ của đầu mũi tên. Theo mặc định, chúng được hiểu là tọa độ Cartesian của các trục.
    • import matplotlib.pyplot as plt
      
      week = [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
      ]
      log_points = [
          3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
          47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
      ]
      
      plt.plot(week, log_points, c='#6caddf', lw=3)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      
      plt.show()
      8 là một tuple chứa tọa độ của văn bản. Một lần nữa, theo mặc định, đây là tọa độ Cartesian.
    • import matplotlib.pyplot as plt
      
      week = [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
      ]
      log_points = [
          3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
          47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
      ]
      
      plt.plot(week, log_points, c='#6caddf', lw=3)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      
      plt.show()
      9 là hệ tọa độ
      import matplotlib.pyplot as plt
      
      week = [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
      ]
      log_points = [
          3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
          47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
      ]
      
      plt.plot(week, log_points, c='#6caddf', lw=3)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      
      plt.show()
      7 (vị trí của đầu mũi tên) được đưa ra. Có một số tùy chọn nhưng có lẽ hữu ích nhất, bên cạnh Cartesian, là
      plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      plt.grid(which='major', c='grey', lw='0.5', linestyle='-')
      
      plt.show()
      1 cho phép bạn chỉ định vị trí là phân số của chiều rộng và chiều cao của khu vực đồ thị. Không giống như hệ tọa độ Cartesian mặc định, tùy chọn này cho phép bạn thêm các chú thích bên ngoài khu vực đồ thị thực tế.
    • plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      plt.grid(which='major', c='grey', lw='0.5', linestyle='-')
      
      plt.show()
      2 tương tự như
      import matplotlib.pyplot as plt
      
      week = [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
      ]
      log_points = [
          3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
          47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
      ]
      
      plt.plot(week, log_points, c='#6caddf', lw=3)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      
      plt.show()
      9 nhưng dành cho văn bản, không phải là đầu mũi tên. Ngoài tất cả các tùy chọn có sẵn cho
      import matplotlib.pyplot as plt
      
      week = [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
      ]
      log_points = [
          3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
          47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
      ]
      
      plt.plot(week, log_points, c='#6caddf', lw=3)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      
      plt.show()
      9, bạn có thể sử dụng
      plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      plt.grid(which='major', c='grey', lw='0.5', linestyle='-')
      
      plt.show()
      5 hoặc
      plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      plt.grid(which='major', c='grey', lw='0.5', linestyle='-')
      
      plt.show()
      6, trong đó chỉ định vị trí của văn bản là một phần bù từ điểm mà nó đang chú thích.
    • plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
      plt.title('Manchester City in the 2018-19 Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      plt.grid(which='major', c='grey', lw='0.5', linestyle='-')
      
      plt.show()
      7 là một từ điển các tùy chọn cho mũi tên
  • Lưu ý rằng việc chú thích có một mũi tên không bắt buộc, điều đó có nghĩa là nếu bạn muốn thêm văn bản nhưng cũng tận dụng quyền kiểm soát lớn hơn mà
    import matplotlib.pyplot as plt
    
    week = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38
    ]
    log_points = [
        3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44, 44,
        47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92, 95, 98
    ]
    
    plt.plot(week, log_points, c='#6caddf', lw=3)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    
    plt.show()
    5 cung cấp cho bạn bạn có thể làm như vậy. Ngược lại, bạn có thể tạo mũi tên mà không cần văn bản bằng cách không chỉ định chuỗi:
plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()

Đọc tài liệu đầy đủ cho nhãn văn bản ở đây và để chú thích ở đây.

Nhiều nhóm

Để vẽ nhiều chuỗi dữ liệu trên cùng một trục, chỉ cần sử dụng hàm

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 13, 100)
y = x**2

plt.plot(x, y)
2 nhiều lần. Khi làm điều này, nó thường tốt nhất để bao gồm một huyền thoại. Điều này được tạo thông qua
plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()
0 sau khi đã chỉ định đối số từ khóa
plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()
1 trong các cuộc gọi
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 13, 100)
y = x**2

plt.plot(x, y)
2:legend. This is created via
plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()
0 after having specified the
plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()
1 keyword argument in the
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 13, 100)
y = x**2

plt.plot(x, y)
2 calls:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team)
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')

Tất nhiên, ví dụ cụ thể này, thật hợp lý khi đặt màu sắc của nhóm:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Create a dictionary of the colours
team_colours = {
    'AFC Bournemouth': '#8b0304',
    'Arsenal': '#ff0000',
    'Brighton': '#005daa',
    'Burnley': '#80bfff',
    'Cardiff': '#224781',
    'Chelsea': '#0000dd',
    'Crystal Palace': '#0a4af5',
    'Everton': '#274488',
    'Fulham': '#000000',
    'Huddersfield': '#176fc0',
    'Leicester': '#0101e8',
    'Liverpool': '#dd0000',
    'Man City': '#6caddf',
    'Man Utd': '#e80909',
    'Newcastle': '#000000',
    'Southampton': '#ed1a3b',
    'Spurs': '#132257',
    'Watford': '#fbee23',
    'West Ham': '#7f0000',
    'Wolves': '#fdbc02'
}

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team, c=team_colours[team])
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')

Định vị huyền thoại bên ngoài các trục

Nó thường có ý nghĩa khi đặt huyền thoại bên ngoài các trục để cho nó thêm một chút phòng. Điều này đòi hỏi một số điều chỉnh kích thước bằng cách sử dụng

plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()
3 và các đối số phức tạp hơn sẽ được chuyển đến
plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()
0:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
0

Đặt hàng thủ công huyền thoại

Nếu bạn muốn chuỗi dữ liệu xuất hiện theo một thứ tự cụ thể trong truyền thuyết, bạn có thể gán cho từng chuỗi chúng cho một biến và sau đó gọi các biến đó trong cuộc gọi

plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()
0. Trong ví dụ này, dữ liệu cho Fulham, Man City và Wolves được vẽ theo thứ tự đó, nhưng thứ tự chúng xuất hiện trong truyền thuyết được xác định thủ công:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
1

Màu bên trong, dưới hoặc giữa các biểu đồ

Có hai phương pháp làm điều này. Đầu tiên -

plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()
6 - sẽ lấy các điểm bạn đang vẽ, vẽ các đường thẳng giữa chúng (bao gồm từ điểm cuối cùng trở lại điểm đầu tiên) và sau đó điền vào khu vực kín đó với màu sắc. Điều này có nghĩa là, để có được phương pháp này hoạt động cho dữ liệu của chúng tôi, chúng tôi cần thêm một điểm bổ sung khi bắt đầu và một điểm bổ sung ở cuối. Chúng sẽ ở (1, 0) và (38, 0), tương ứng:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
2

Điều này rõ ràng là khá khó hiểu, vì vậy một tùy chọn đơn giản hơn sẽ là sử dụng

plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
plt.title('Manchester City in the 2018-19 Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlim(1, 38)
# Add text and annotations
plt.text(33, 62, 'Final 14 matches', ha='center')
plt.text(33, 58, 'were all victories', ha='center')
plt.annotate(
    'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
    arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
)
plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
plt.annotate(
    '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
    arrowprops=dict(arrowstyle="<->", color='k')
)
plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')

plt.show()
7, lấp đầy khu vực giữa các đường có màu. Nếu bạn chỉ chỉ định một dòng thì nó sẽ mặc định giả định rằng dòng thứ hai là trục x:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
3

Và sau đó, tất nhiên, bạn vẫn có tùy chọn chỉ định dòng thứ hai để điền vào khu vực giữa hai:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
4
  • Sử dụng
    plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlim(1, 38)
    # Add text and annotations
    plt.text(33, 62, 'Final 14 matches', ha='center')
    plt.text(33, 58, 'were all victories', ha='center')
    plt.annotate(
        'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
        arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
    )
    plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
    plt.annotate(
        '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
        arrowprops=dict(arrowstyle="<->", color='k')
    )
    plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')
    
    plt.show()
    8 để làm điều tương tự như trên nhưng giữa hai đường cong dọc (hoặc giữa đường cong và trục y)vertical curves (or between a curve and the y-axis)
  • Sử dụng đối số từ khóa
    plt.plot(week, log_points, c='#6caddf', lw=3, alpha=0.5)
    plt.title('Manchester City in the 2018-19 Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlim(1, 38)
    # Add text and annotations
    plt.text(33, 62, 'Final 14 matches', ha='center')
    plt.text(33, 58, 'were all victories', ha='center')
    plt.annotate(
        'Moved clear at the top of the table', (11, 29), (30, -20), textcoords='offset points',
        arrowprops=dict(facecolor='black', width=0.5, headwidth=5, headlength=7)
    )
    plt.annotate('Week 11', (11, 29), (-6, 6), textcoords='offset points', ha='right')
    plt.annotate(
        '', (16 / 38, -0.07), (28 / 38, -0.07), 'axes fraction', 'axes fraction',
        arrowprops=dict(arrowstyle="<->", color='k')
    )
    plt.annotate('Dropped from top spot', (22 / 38, -0.12), xycoords='axes fraction', ha='center')
    
    plt.show()
    9 để áp dụng một mẫu cho khu vực bóng mờ. Để tăng mật độ của bóng, hãy tăng số lượng ký tự khi đặt mẫu nở (xem ở đây để biết thêm ví dụ):apply a pattern to the shaded area. To increase the density of the shading, increase the number of characters when setting the hatch pattern (see here for more examples):
import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
5

Tùy chỉnh nhãn trục

Chỉ định các vị trí và nhãn đánh dấu chính xác

Sử dụng

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team)
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')
0 và
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team)
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')
1 để:

  • Xác định vị trí của trục trục
  • Liệt kê các chuỗi chính xác sẽ được sử dụng làm nhãn
import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
6

Âm mưu văn bản

Trong ví dụ trước, biểu đồ được tạo bằng cách sử dụng các số trên trục X sau đó được thay thế bằng nhãn văn bản theo cách thủ công thông qua hàm

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team)
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')
1. Tuy nhiên, trong ví dụ này, chúng tôi đã cắt bỏ Middman, và sử dụng các chuỗi văn bản làm dữ liệu X ngay từ đầu:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
7

Dữ liệu trên đến từ Wikipedia.

Mở rộng quy mô và định dạng nhãn đánh dấu

Nếu bạn muốn thay đổi thang đo trên một trục, thực tiễn tốt nhất là chỉnh sửa các giá trị ngay khi chúng được chuyển vào hàm trái ngược với việc tạo một biến mới. Ví dụ, các số tạo ra dữ liệu X của chúng tôi là trong 'tuần' nhưng nếu chúng tôi muốn mở rộng chúng đến 'ngày', chúng tôi chỉ cần chuyển đổi danh sách thành một mảng và sau đó nhân nó với 7. Điều này sẽ được thực hiện Ngay trong đối số của cuộc gọi

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 13, 100)
y = x**2

plt.plot(x, y)
2 thay vì xác định một biến mới:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
8

Trên đây là tốt hơn là làm những điều sau nếu tất cả những gì bạn muốn làm là tỷ lệ lại trục:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
9

Lưu ý rằng định dạng của các số trên trục có thể được thay đổi thành ký hiệu khoa học bằng cách sử dụng hàm

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team)
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')
4.

Sử dụng quy mô nhật ký

Nếu bạn muốn sử dụng thang đo logarit, hãy xem hàm

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team)
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')
5,
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team)
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')
6 hoặc
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team)
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')
7, tùy thuộc vào trục/trục bạn muốn thay đổi:

import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
0

Nhãn trục quay

Điều này có thể được thực hiện bằng cách sử dụng hàm

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team)
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')
8:

import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
1

Biểu đồ phụ

Để tạo hai (hoặc nhiều hơn) các lô hoàn toàn riêng biệt trong cùng một hình, bạn sẽ cần phải tạo các ô phụ:sub-plots:

  • import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    # Get the data
    data = pd.read_csv('Week-by-Week Points.csv')
    
    # Plot
    for team in list(data):
        log_points = data[team]
        week = np.arange(1, 39)
        plt.plot(week, log_points, label=team)
    plt.title('2018-19 English Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    plt.legend(fontsize='xx-small')
    9 sẽ tạo một đối tượng âm mưu phụ trong đó
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    # Get the data
    data = pd.read_csv('Week-by-Week Points.csv')
    
    # Create a dictionary of the colours
    team_colours = {
        'AFC Bournemouth': '#8b0304',
        'Arsenal': '#ff0000',
        'Brighton': '#005daa',
        'Burnley': '#80bfff',
        'Cardiff': '#224781',
        'Chelsea': '#0000dd',
        'Crystal Palace': '#0a4af5',
        'Everton': '#274488',
        'Fulham': '#000000',
        'Huddersfield': '#176fc0',
        'Leicester': '#0101e8',
        'Liverpool': '#dd0000',
        'Man City': '#6caddf',
        'Man Utd': '#e80909',
        'Newcastle': '#000000',
        'Southampton': '#ed1a3b',
        'Spurs': '#132257',
        'Watford': '#fbee23',
        'West Ham': '#7f0000',
        'Wolves': '#fdbc02'
    }
    
    # Plot
    for team in list(data):
        log_points = data[team]
        week = np.arange(1, 39)
        plt.plot(week, log_points, label=team, c=team_colours[team])
    plt.title('2018-19 English Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    plt.legend(fontsize='xx-small')
    0 là tổng số hàng các hàng bạn dự định sẽ thực hiện,
    # Plot a straight diagonal line
    plt.plot([0, 9], [0, 8])
    # Plot a horizontal line
    plt.axhline(4)
    # Plot a vertical line
    plt.axvline(7)
    1 là số lượng các cột bạn dự định và
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    # Get the data
    data = pd.read_csv('Week-by-Week Points.csv')
    
    # Create a dictionary of the colours
    team_colours = {
        'AFC Bournemouth': '#8b0304',
        'Arsenal': '#ff0000',
        'Brighton': '#005daa',
        'Burnley': '#80bfff',
        'Cardiff': '#224781',
        'Chelsea': '#0000dd',
        'Crystal Palace': '#0a4af5',
        'Everton': '#274488',
        'Fulham': '#000000',
        'Huddersfield': '#176fc0',
        'Leicester': '#0101e8',
        'Liverpool': '#dd0000',
        'Man City': '#6caddf',
        'Man Utd': '#e80909',
        'Newcastle': '#000000',
        'Southampton': '#ed1a3b',
        'Spurs': '#132257',
        'Watford': '#fbee23',
        'West Ham': '#7f0000',
        'Wolves': '#fdbc02'
    }
    
    # Plot
    for team in list(data):
        log_points = data[team]
        week = np.arange(1, 39)
        plt.plot(week, log_points, label=team, c=team_colours[team])
    plt.title('2018-19 English Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    plt.legend(fontsize='xx-small')
    2 là số mà lô này sẽ nằm trong lưới của các lô . Ví dụ:
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    # Get the data
    data = pd.read_csv('Week-by-Week Points.csv')
    
    # Create a dictionary of the colours
    team_colours = {
        'AFC Bournemouth': '#8b0304',
        'Arsenal': '#ff0000',
        'Brighton': '#005daa',
        'Burnley': '#80bfff',
        'Cardiff': '#224781',
        'Chelsea': '#0000dd',
        'Crystal Palace': '#0a4af5',
        'Everton': '#274488',
        'Fulham': '#000000',
        'Huddersfield': '#176fc0',
        'Leicester': '#0101e8',
        'Liverpool': '#dd0000',
        'Man City': '#6caddf',
        'Man Utd': '#e80909',
        'Newcastle': '#000000',
        'Southampton': '#ed1a3b',
        'Spurs': '#132257',
        'Watford': '#fbee23',
        'West Ham': '#7f0000',
        'Wolves': '#fdbc02'
    }
    
    # Plot
    for team in list(data):
        log_points = data[team]
        week = np.arange(1, 39)
        plt.plot(week, log_points, label=team, c=team_colours[team])
    plt.title('2018-19 English Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    plt.legend(fontsize='xx-small')
    3 sẽ chia hình của bạn thành một lưới với 3 hàng và 2 cột (tức là không gian cho 6 lô) và sau đó tạo một âm mưu phụ trống cho phần đầu tiên (trên cùng bên trái) của các lô này. Các lô được đánh số bằng cách sử dụng ‘Đọc thứ tự (từ trái sang phải, từ trên xuống dưới), IE Plot 2 sẽ là đỉnh cao bên phải, lô 3 sẽ là giữa bên trái, v.v.
  • Để tạo ra đủ không gian cho tất cả các lô này, đó là một ý tưởng tốt để kích thước lại hình dáng của bạn. Chủ đề này được thảo luận trên trang riêng của riêng nó, nhưng tóm lại, các tùy chọn của bạn là:
    • import matplotlib.pyplot as plt
      import pandas as pd
      import numpy as np
      
      # Get the data
      data = pd.read_csv('Week-by-Week Points.csv')
      
      # Create a dictionary of the colours
      team_colours = {
          'AFC Bournemouth': '#8b0304',
          'Arsenal': '#ff0000',
          'Brighton': '#005daa',
          'Burnley': '#80bfff',
          'Cardiff': '#224781',
          'Chelsea': '#0000dd',
          'Crystal Palace': '#0a4af5',
          'Everton': '#274488',
          'Fulham': '#000000',
          'Huddersfield': '#176fc0',
          'Leicester': '#0101e8',
          'Liverpool': '#dd0000',
          'Man City': '#6caddf',
          'Man Utd': '#e80909',
          'Newcastle': '#000000',
          'Southampton': '#ed1a3b',
          'Spurs': '#132257',
          'Watford': '#fbee23',
          'West Ham': '#7f0000',
          'Wolves': '#fdbc02'
      }
      
      # Plot
      for team in list(data):
          log_points = data[team]
          week = np.arange(1, 39)
          plt.plot(week, log_points, label=team, c=team_colours[team])
      plt.title('2018-19 English Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      plt.legend(fontsize='xx-small')
      4 để đặt chiều rộng và chiều cao của con số bạn hiện đang làm việc
    • import matplotlib.pyplot as plt
      import pandas as pd
      import numpy as np
      
      # Get the data
      data = pd.read_csv('Week-by-Week Points.csv')
      
      # Create a dictionary of the colours
      team_colours = {
          'AFC Bournemouth': '#8b0304',
          'Arsenal': '#ff0000',
          'Brighton': '#005daa',
          'Burnley': '#80bfff',
          'Cardiff': '#224781',
          'Chelsea': '#0000dd',
          'Crystal Palace': '#0a4af5',
          'Everton': '#274488',
          'Fulham': '#000000',
          'Huddersfield': '#176fc0',
          'Leicester': '#0101e8',
          'Liverpool': '#dd0000',
          'Man City': '#6caddf',
          'Man Utd': '#e80909',
          'Newcastle': '#000000',
          'Southampton': '#ed1a3b',
          'Spurs': '#132257',
          'Watford': '#fbee23',
          'West Ham': '#7f0000',
          'Wolves': '#fdbc02'
      }
      
      # Plot
      for team in list(data):
          log_points = data[team]
          week = np.arange(1, 39)
          plt.plot(week, log_points, label=team, c=team_colours[team])
      plt.title('2018-19 English Premier League')
      plt.ylabel('Log Points')
      plt.ylim(0, 100)
      plt.xlabel('Week')
      plt.xlim(1, 38)
      plt.legend(fontsize='xx-small')
      5 để đặt tham số hình tương tự như trên nhưng đối với tất cả các số liệu trong mã của bạn
  • Bởi vì
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 13, 100)
    y = x**2
    
    plt.plot(x, y)
    6 tạo ra một tiêu đề cho một lô riêng lẻ, để tạo ra một tiêu đề cho toàn bộ con số bạn cần sử dụng
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    # Get the data
    data = pd.read_csv('Week-by-Week Points.csv')
    
    # Create a dictionary of the colours
    team_colours = {
        'AFC Bournemouth': '#8b0304',
        'Arsenal': '#ff0000',
        'Brighton': '#005daa',
        'Burnley': '#80bfff',
        'Cardiff': '#224781',
        'Chelsea': '#0000dd',
        'Crystal Palace': '#0a4af5',
        'Everton': '#274488',
        'Fulham': '#000000',
        'Huddersfield': '#176fc0',
        'Leicester': '#0101e8',
        'Liverpool': '#dd0000',
        'Man City': '#6caddf',
        'Man Utd': '#e80909',
        'Newcastle': '#000000',
        'Southampton': '#ed1a3b',
        'Spurs': '#132257',
        'Watford': '#fbee23',
        'West Ham': '#7f0000',
        'Wolves': '#fdbc02'
    }
    
    # Plot
    for team in list(data):
        log_points = data[team]
        week = np.arange(1, 39)
        plt.plot(week, log_points, label=team, c=team_colours[team])
    plt.title('2018-19 English Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    plt.legend(fontsize='xx-small')
    7
  • Theo mặc định, bố cục của các ô trong một lưới các ô phụ không sử dụng hết không gian có sẵn đặc biệt tốt. Điều này có thể được cải thiện bằng cách sử dụng
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    # Get the data
    data = pd.read_csv('Week-by-Week Points.csv')
    
    # Create a dictionary of the colours
    team_colours = {
        'AFC Bournemouth': '#8b0304',
        'Arsenal': '#ff0000',
        'Brighton': '#005daa',
        'Burnley': '#80bfff',
        'Cardiff': '#224781',
        'Chelsea': '#0000dd',
        'Crystal Palace': '#0a4af5',
        'Everton': '#274488',
        'Fulham': '#000000',
        'Huddersfield': '#176fc0',
        'Leicester': '#0101e8',
        'Liverpool': '#dd0000',
        'Man City': '#6caddf',
        'Man Utd': '#e80909',
        'Newcastle': '#000000',
        'Southampton': '#ed1a3b',
        'Spurs': '#132257',
        'Watford': '#fbee23',
        'West Ham': '#7f0000',
        'Wolves': '#fdbc02'
    }
    
    # Plot
    for team in list(data):
        log_points = data[team]
        week = np.arange(1, 39)
        plt.plot(week, log_points, label=team, c=team_colours[team])
    plt.title('2018-19 English Premier League')
    plt.ylabel('Log Points')
    plt.ylim(0, 100)
    plt.xlabel('Week')
    plt.xlim(1, 38)
    plt.legend(fontsize='xx-small')
    8
import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
2

Kích thước mủ và hình ảnh

Xem ở đây để biết thêm về việc sử dụng định dạng latex trong nhãn Tiêu đề và Axes và xem ở đây để biết thêm về việc thay đổi kích thước hình ảnh.

import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
3

Hoàn thành?

Cuối cùng, hãy lưu cốt truyện dưới dạng PNG, JPG, PDF hoặc loại hình ảnh khác với

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Get the data
data = pd.read_csv('Week-by-Week Points.csv')

# Create a dictionary of the colours
team_colours = {
    'AFC Bournemouth': '#8b0304',
    'Arsenal': '#ff0000',
    'Brighton': '#005daa',
    'Burnley': '#80bfff',
    'Cardiff': '#224781',
    'Chelsea': '#0000dd',
    'Crystal Palace': '#0a4af5',
    'Everton': '#274488',
    'Fulham': '#000000',
    'Huddersfield': '#176fc0',
    'Leicester': '#0101e8',
    'Liverpool': '#dd0000',
    'Man City': '#6caddf',
    'Man Utd': '#e80909',
    'Newcastle': '#000000',
    'Southampton': '#ed1a3b',
    'Spurs': '#132257',
    'Watford': '#fbee23',
    'West Ham': '#7f0000',
    'Wolves': '#fdbc02'
}

# Plot
for team in list(data):
    log_points = data[team]
    week = np.arange(1, 39)
    plt.plot(week, log_points, label=team, c=team_colours[team])
plt.title('2018-19 English Premier League')
plt.ylabel('Log Points')
plt.ylim(0, 100)
plt.xlabel('Week')
plt.xlim(1, 38)
plt.legend(fontsize='xx-small')
9 hoặc hiển thị nó trong cửa sổ bật lên với
# Plot a straight diagonal line
plt.plot([0, 9], [0, 8])
# Plot a horizontal line
plt.axhline(4)
# Plot a vertical line
plt.axvline(7)
4:

import matplotlib.pyplot as plt

x = [1, 5, 9, 13]
y = [1, 25, 81, 169]

plt.plot(x, y)
4

Nếu bạn đang vẽ nhiều hơn một hình trong cùng một tập lệnh Python, hãy sử dụng

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
01 và
import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()
02 trước và sau mỗi lần, để nói với Python khi một âm mưu kết thúc và hình tiếp theo bắt đầu.

Làm thế nào để bạn vẽ một đường chéo trong Python?

Vẽ một đường chéo dựa trên phía dưới bên trái đến đỉnh bên phải của màn hình khá đơn giản, bạn chỉ có thể sử dụng ax.plot (ax.get_xlim (), ax.get_ylim (), ls = "-", c = ".ax. plot(ax. get_xlim(), ax. get_ylim(), ls="--", c=".

Làm thế nào để tôi vẽ một dòng trong Python?

Sơ đồ dòng đơn giản..
%matplotlib nhập nội tuyến nhập matplotlib.pyplot như plt plt.Phong cách.Sử dụng ('Seaborn-Whitegrid') Nhập khẩu NUMPY dưới dạng NP.....
FIG = plt.Hình () ax = plt.trục () ....
Trong [3]: fig = plt.Hình () ax = plt.....
Trong [4]: plt.Lô đất (x, np. ....
Trong [5]: plt.Lô đất (x, np. ....
plt.Biểu đồ (x, x + 0, '-g') # plt màu xanh lá cây rắn.....
Trong [9]: plt.....
Trong [10]: plt ..

Làm thế nào để bạn vẽ một đường ngang trong Python?

Làm thế nào để bạn vẽ một đường ngang thẳng trong Python?Sử dụng plt.plot () để vẽ một đường thẳng ngang PLT.Biểu đồ (x, y) với x như là một chuỗi các tọa độ x khác nhau và y như một chuỗi tọa độ y bằng nhau để vẽ một đường ngang.Use plt. plot() to plot a horizontal line Call plt. plot(x, y) with x as a sequence of differing x-coordinates and y as a sequence of equal y-coordinates to draw a horizontal line.

Cốt truyện () làm gì trong Python?

Hàm lô () được sử dụng để vẽ các điểm (điểm đánh dấu) trong sơ đồ.Theo mặc định, hàm lô () vẽ một dòng từ điểm này sang điểm khác.Hàm lấy các tham số để chỉ định các điểm trong sơ đồ.draw points (markers) in a diagram. By default, the plot() function draws a line from point to point. The function takes parameters for specifying points in the diagram.