Hướng dẫn plot horizontal line python - âm mưu trăn đường ngang

Xem thảo luận

Show

Nội dung chính ShowShow

  • 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.
  • import matplotlib.pyplot as plt
  • Vẽ nhiều đường ngang
  • Để vẽ nhiều dòng ngang, hãy sử dụng phương thức Axhline () nhiều lần.
  • 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:
  • 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) 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
  • Cốt truyện ở trục biển
  • Cốt truyện ở mức độ biển
  • Trục thời gian
  • Barplot và biểu đồ
  • 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?
  • Làm thế nào để tôi vẽ một dòng trong Python?

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()

    Bàn luậnaxhline()axhline() function in pyplot module of matplotlib library is used to add a horizontal line across the axis.

    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.matplotlib.pyplot.axhline(y, color, xmin, xmax, linestyle)

    Parameters:

    • Vẽ một đường ngang khá đơn giản, sử dụng axhline ()matplotlib.pyplot.axhline(y, color, xmin, xmax, linestyle) Position on Y axis to plot  the line, It accepts integers.
    • 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.scalar, optional, default: 0/1.  It plots the line in the given range
    • Cú pháp: matplotlib.pyplot.axhline (y, color, xmin, xmax, lineStyle)scalar, optional, default: 0/1.  It plots the line in the given range color for the line, It accepts  a string. eg ‘r’ or ‘b’ .
    • 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’ .Specifies 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

    import matplotlib.pyplot as plt

    Vẽ nhiều đường ngang

    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 plot horizontal line python - âm mưu trăn đường ngang

    import matplotlib.pyplot as plt

    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()
    
    5

    Vẽ nhiều đường ngang

    Vẽ nhiều đường ngang

    import matplotlib.pyplot as plt

    Vẽ nhiều đường ngang

    Để vẽ nhiều dòng ngang, hãy sử dụng phương thức Axhline () nhiều lầ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()
    
    5

    Vẽ nhiều đường ngang

    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:

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

    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:legend() function.

    Python3

    import matplotlib.pyplot as plt

    Vẽ nhiều đường ngang

    Để vẽ nhiều dòng ngang, hãy sử dụng phương thức Axhline () nhiều lầ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()
    
    5

    Vẽ nhiều đường ngang

    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.

    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:

    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:


    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:

    • 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) 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
    • Cốt truyện ở trục biển
    • Cốt truyện ở mức độ biển
    • Trục thời gian
      • Barplot và biể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()
      
      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)
    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()
    
    3

    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()
    

    Cốt truyện ở trục biể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()
    
    5

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

    • Mỗi trục phải được lặp lại thông qua
    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()
    
    6

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

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

    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')
    

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