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

Xem thảo luận

Show

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luậnaxhline()

    Matplotlib là một thư viện Python phổ biến được sử dụng để vẽ, nó cung cấp một API hướng đối tượng để hiển thị các lô GUIaxhline() function in pyplot module of matplotlib library is used to add a horizontal line across the axis.

    Vẽ một đường ngang khá đơn giản, sử dụng axhline ()matplotlib.pyplot.axhline(y, color, xmin, xmax, linestyle)

    Parameters:

    • Hàm axhline () trong mô -đun pyplot của thư viện matplotlib được sử dụng để thêm một đường ngang trên trục. Position on Y axis to plot  the line, It accepts integers.
    • Cú pháp: matplotlib.pyplot.axhline (y, color, xmin, xmax, lineStyle)scalar, optional, default: 0/1.  It plots the line in the given range
    • y: vị trí trên trục y để vẽ & nbsp; dòng, nó chấp nhận số nguyên. color for the line, It accepts  a string. eg ‘r’ or ‘b’ .
    • xmin và xmax: vô hướng, tùy chọn, mặc định: 0/1. & nbsp; nó biểu thị dòng trong phạm vi đã choSpecifies the type of line, It accepts a string. eg ‘-‘, ‘–‘, ‘-.’, ‘:’, ‘None’, ‘ ‘, ”, ‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’

    Màu sắc: Màu sắc cho dòng, nó chấp nhận & nbsp; một chuỗi. ví dụ: ’r, hoặc’ bv.

    Python3

    LineStyle: Chỉ định loại dòng, nó chấp nhận một chuỗi. Ví dụ: ‘-‘, ‘-‘, ‘-.

    Vẽ một đường ngang duy nhất

    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    9

    Output:

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

    import matplotlib.pyplot as plt

    plt.axhline(y

    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    0
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    1
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    2
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    0

    Vẽ nhiều đường ngang

    LineStyle: Chỉ định loại dòng, nó chấp nhận một chuỗi. Ví dụ: ‘-‘, ‘-‘, ‘-.

    Vẽ một đường ngang duy nhất

    import matplotlib.pyplot as plt

    plt.axhline(y

    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    0
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    1
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    2
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    0

    Vẽ nhiều đường ngang

    import pandas as pd
    import seaborn as sns  # for tips data
    
    # load data
    tips = sns.load_dataset('tips')
    
    # histogram
    ax = tips.plot(kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Horizontal Line')
    _ = ax.hlines(y=6, xmin=0, xmax=55, colors='r')
    
    # barplot 
    ax = tips.loc[5:25, ['total_bill', 'tip']].plot(kind='bar', figsize=(15, 4), title='Barplot with Vertical Lines', rot=0)
    _ = ax.hlines(y=6, xmin=3, xmax=15, colors='r')
    
    3
    import pandas as pd
    import seaborn as sns  # for tips data
    
    # load data
    tips = sns.load_dataset('tips')
    
    # histogram
    ax = tips.plot(kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Horizontal Line')
    _ = ax.hlines(y=6, xmin=0, xmax=55, colors='r')
    
    # barplot 
    ax = tips.loc[5:25, ['total_bill', 'tip']].plot(kind='bar', figsize=(15, 4), title='Barplot with Vertical Lines', rot=0)
    _ = ax.hlines(y=6, xmin=3, xmax=15, colors='r')
    
    4
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    8

    import pandas as pd
    import seaborn as sns  # for tips data
    
    # load data
    tips = sns.load_dataset('tips')
    
    # histogram
    ax = tips.plot(kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Horizontal Line')
    _ = ax.hlines(y=6, xmin=0, xmax=55, colors='r')
    
    # barplot 
    ax = tips.loc[5:25, ['total_bill', 'tip']].plot(kind='bar', figsize=(15, 4), title='Barplot with Vertical Lines', rot=0)
    _ = ax.hlines(y=6, xmin=3, xmax=15, colors='r')
    
    6
    import pandas as pd
    import seaborn as sns  # for tips data
    
    # load data
    tips = sns.load_dataset('tips')
    
    # histogram
    ax = tips.plot(kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Horizontal Line')
    _ = ax.hlines(y=6, xmin=0, xmax=55, colors='r')
    
    # barplot 
    ax = tips.loc[5:25, ['total_bill', 'tip']].plot(kind='bar', figsize=(15, 4), title='Barplot with Vertical Lines', rot=0)
    _ = ax.hlines(y=6, xmin=3, xmax=15, colors='r')
    
    7
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    8

    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    9

    Output:

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

    Để vẽ nhiều dòng ngang, hãy sử dụng phương thức Axhline () nhiều lần.

    Pythonlegend() function.

    Python3

    LineStyle: Chỉ định loại dòng, nó chấp nhận một chuỗi. Ví dụ: ‘-‘, ‘-‘, ‘-.

    Vẽ một đường ngang duy nhất

    import matplotlib.pyplot as plt

    plt.axhline(y

    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    0
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    1
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    2
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    0

    Vẽ nhiều đường ngang

    import pandas as pd
    import seaborn as sns  # for tips data
    
    # load data
    tips = sns.load_dataset('tips')
    
    # histogram
    ax = tips.plot(kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Horizontal Line')
    _ = ax.hlines(y=6, xmin=0, xmax=55, colors='r')
    
    # barplot 
    ax = tips.loc[5:25, ['total_bill', 'tip']].plot(kind='bar', figsize=(15, 4), title='Barplot with Vertical Lines', rot=0)
    _ = ax.hlines(y=6, xmin=3, xmax=15, colors='r')
    
    3
    import pandas as pd
    import seaborn as sns  # for tips data
    
    # load data
    tips = sns.load_dataset('tips')
    
    # histogram
    ax = tips.plot(kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Horizontal Line')
    _ = ax.hlines(y=6, xmin=0, xmax=55, colors='r')
    
    # barplot 
    ax = tips.loc[5:25, ['total_bill', 'tip']].plot(kind='bar', figsize=(15, 4), title='Barplot with Vertical Lines', rot=0)
    _ = ax.hlines(y=6, xmin=3, xmax=15, colors='r')
    
    4
    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    8

    Để vẽ nhiều dòng ngang, hãy sử dụng phương thức Axhline () nhiều lần.

    Python

    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    
    9

    Output:

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


    Sử dụng import numpy as np import matplotlib.pyplot as plt xs = np.linspace(1, 21, 200) fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6)) ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2) ax1.set_title('One Line') ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2) ax2.set_title('Multiple Lines') plt.tight_layout() plt.show() 39:

    • Các phương pháp này được áp dụng cho các lô được tạo bằng
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      40 và
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      41, cả hai đều sử dụng
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      42.
    • Vẽ nhiều đường ngang bằng cách chuyển
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      43 đến tham số
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      44.
    • import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      44 có thể được thông qua như một địa điểm duy nhất:
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      46
    • import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      44 có thể được thông qua dưới dạng nhiều địa điểm:
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      48
    • Ngoài ra
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      49 cho API hướng đối tượng.
      • Nếu bạn là một người vẽ một hình với một cái gì đó như
        import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        50, thì hãy thay thế
        import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        51 hoặc
        import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        52 bằng
        import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        53 hoặc
        import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        54, tương ứng.
    • import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      55 &
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      56 chỉ có thể vẽ một vị trí duy nhất (ví dụ:
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      46)
    • Xem câu trả lời này cho các đường thẳng đứng với
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      58

    import numpy as np import matplotlib.pyplot as plt xs = np.linspace(1, 21, 200) fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6)) ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2) ax1.set_title('One Line') ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2) ax2.set_title('Multiple Lines') plt.tight_layout() plt.show() 59

    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    
    plt.figure(figsize=(6, 3))
    plt.hlines(y=39.5, xmin=100, xmax=175, colors='aqua', linestyles='-', lw=2, label='Single Short Line')
    plt.hlines(y=[39, 40, 41], xmin=[0, 25, 50], xmax=[len(xs)], colors='purple', linestyles='--', lw=2, label='Multiple Lines')
    plt.legend(bbox_to_anchor=(1.04,0.5), loc="center left", borderaxespad=0)
    

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

    import numpy as np import matplotlib.pyplot as plt xs = np.linspace(1, 21, 200) fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6)) ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2) ax1.set_title('One Line') ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2) ax2.set_title('Multiple Lines') plt.tight_layout() plt.show() 60

    import numpy as np
    import matplotlib.pyplot as plt
    
    xs = np.linspace(1, 21, 200)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
    
    ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
    ax1.set_title('One Line')
    
    ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
    ax2.set_title('Multiple Lines')
    
    plt.tight_layout()
    plt.show()
    

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

    Cốt truyện ở trục biển

    import seaborn as sns
    
    # sample data
    fmri = sns.load_dataset("fmri")
    
    # max y values for stim and cue
    c_max, s_max = fmri.pivot_table(index='timepoint', columns='event', values='signal', aggfunc='mean').max()
    
    # plot
    g = sns.lineplot(data=fmri, x="timepoint", y="signal", hue="event")
    
    # x min and max
    xmin, ymax = g.get_xlim()
    
    # vertical lines
    g.hlines(y=[c_max, s_max], xmin=xmin, xmax=xmax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2)
    

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

    Cốt truyện ở mức độ biển

    • Mỗi trục phải được lặp lại thông qua
    import seaborn as sns
    
    # sample data
    fmri = sns.load_dataset("fmri")
    
    # used to get the max values (y) for each event in each region
    fpt = fmri.pivot_table(index=['region', 'timepoint'], columns='event', values='signal', aggfunc='mean')
    
    # plot
    g = sns.relplot(data=fmri, x="timepoint", y="signal", col="region",hue="event", style="event", kind="line")
    
    # iterate through the axes
    for ax in g.axes.flat:
        # get x min and max
        xmin, xmax = ax.get_xlim()  
        # extract the region from the title for use in selecting the index of fpt
        region = ax.get_title().split(' = ')[1]  
        # get x values for max event
        c_max, s_max = fpt.loc[region].max() 
        # add horizontal lines 
        ax.hlines(y=[c_max, s_max], xmin=xmin, xmax=xmax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5)
    

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

    Trục thời gian

    • import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      61 và
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      62 sẽ chấp nhận một ngày như
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      63 hoặc
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      64
      • Sử dụng
        import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        65
      • import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        66
      • Cho
        import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        67,
        import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        68, trong đó chỉ số là
        import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        69.
    • Cột ngày trên trục phải là
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      70. Nếu sử dụng gấu trúc, thì hãy sử dụng
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      71. Đối với một mảng hoặc danh sách, hãy tham khảo chuyển đổi mảng chuỗi numpy thành DateTime hoặc chuyển đổi danh sách DateTime thành Python ngày.
    import pandas_datareader as web  # conda or pip install this; not part of pandas
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # get test data; the Date index is already downloaded as datetime dtype
    df = web.DataReader('^gspc', data_source='yahoo', start='2020-09-01', end='2020-09-28').iloc[:, :2]
    
    # display(df.head(2))
                       High          Low
    Date                                
    2020-09-01  3528.030029  3494.600098
    2020-09-02  3588.110107  3535.229980
    
    # plot dataframe
    ax = df.plot(figsize=(9, 6), title='S&P 500', ylabel='Price')
    
    # add horizontal line
    ax.hlines(y=3450, xmin='2020-09-10', xmax='2020-09-17', color='purple', label='test')
    
    ax.legend()
    plt.show()
    

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

    • Dữ liệu chuỗi thời gian mẫu nếu
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      72 không hoạt động.
    data = {pd.Timestamp('2020-09-01 00:00:00'): {'High': 3528.03, 'Low': 3494.6}, pd.Timestamp('2020-09-02 00:00:00'): {'High': 3588.11, 'Low': 3535.23}, pd.Timestamp('2020-09-03 00:00:00'): {'High': 3564.85, 'Low': 3427.41}, pd.Timestamp('2020-09-04 00:00:00'): {'High': 3479.15, 'Low': 3349.63}, pd.Timestamp('2020-09-08 00:00:00'): {'High': 3379.97, 'Low': 3329.27}, pd.Timestamp('2020-09-09 00:00:00'): {'High': 3424.77, 'Low': 3366.84}, pd.Timestamp('2020-09-10 00:00:00'): {'High': 3425.55, 'Low': 3329.25}, pd.Timestamp('2020-09-11 00:00:00'): {'High': 3368.95, 'Low': 3310.47}, pd.Timestamp('2020-09-14 00:00:00'): {'High': 3402.93, 'Low': 3363.56}, pd.Timestamp('2020-09-15 00:00:00'): {'High': 3419.48, 'Low': 3389.25}, pd.Timestamp('2020-09-16 00:00:00'): {'High': 3428.92, 'Low': 3384.45}, pd.Timestamp('2020-09-17 00:00:00'): {'High': 3375.17, 'Low': 3328.82}, pd.Timestamp('2020-09-18 00:00:00'): {'High': 3362.27, 'Low': 3292.4}, pd.Timestamp('2020-09-21 00:00:00'): {'High': 3285.57, 'Low': 3229.1}, pd.Timestamp('2020-09-22 00:00:00'): {'High': 3320.31, 'Low': 3270.95}, pd.Timestamp('2020-09-23 00:00:00'): {'High': 3323.35, 'Low': 3232.57}, pd.Timestamp('2020-09-24 00:00:00'): {'High': 3278.7, 'Low': 3209.45}, pd.Timestamp('2020-09-25 00:00:00'): {'High': 3306.88, 'Low': 3228.44}, pd.Timestamp('2020-09-28 00:00:00'): {'High': 3360.74, 'Low': 3332.91}}
    
    df = pd.DataFrame.from_dict(data, 'index')
    

    Barplot và biểu đồ

    • Lưu ý rằng các vị trí đánh dấu âm thanh của thanh có chỉ mục dựa trên không, bất kể nhãn đánh dấu trục, vì vậy hãy chọn
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      61 và
      import numpy as np
      import matplotlib.pyplot as plt
      
      xs = np.linspace(1, 21, 200)
      fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
      
      ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
      ax1.set_title('One Line')
      
      ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
      ax2.set_title('Multiple Lines')
      
      plt.tight_layout()
      plt.show()
      
      62 dựa trên chỉ mục thanh, chứ không phải nhãn Tick.
      • import numpy as np
        import matplotlib.pyplot as plt
        
        xs = np.linspace(1, 21, 200)
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
        
        ax1.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
        ax1.set_title('One Line')
        
        ax2.hlines(y=[39, 40, 41], xmin=0, xmax=len(xs), colors='purple', linestyles='--', lw=2)
        ax2.set_title('Multiple Lines')
        
        plt.tight_layout()
        plt.show()
        
        75 sẽ hiển thị các vị trí và nhãn.
    import pandas as pd
    import seaborn as sns  # for tips data
    
    # load data
    tips = sns.load_dataset('tips')
    
    # histogram
    ax = tips.plot(kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Horizontal Line')
    _ = ax.hlines(y=6, xmin=0, xmax=55, colors='r')
    
    # barplot 
    ax = tips.loc[5:25, ['total_bill', 'tip']].plot(kind='bar', figsize=(15, 4), title='Barplot with Vertical Lines', rot=0)
    _ = ax.hlines(y=6, xmin=3, xmax=15, colors='r')
    

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

    Hướng dẫn how do you plot a horizontal line in python? - 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 trong Python?

    Trong matplotlib, phương thức axHline () được sử dụng để vẽ các đường ngang vào lô ...
    Y: Chỉ định vị trí trên trục y để vẽ đường dây ..
    XMIN và XMAX: Chỉ định phạm vi bắt đầu và kết thúc của dòng ..
    Màu sắc: Chỉ định màu của dòng ..
    LineStyle: Chỉ định kiểu của dòng ..

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

    Các đường ngang có độ dốc bằng không.Để đồ thị một đường ngang trong hệ tọa độ tiêu chuẩn, hãy sử dụng phương trình y = k, y = k, y = k, trong đó k đưa ra điểm trên trục y mà đường sẽ giao nhau.use the equation y = k , y = k , y=k, where k gives the point on the y-axis that the line will intersect.

    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 ..