Xem thảo luận Nội dung chính ShowShow
Cải thiện bài viết
Lưu bài viết
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[]
9Output:
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[]
5Vẽ nhiều đường ngangVẽ 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[]
5Vẽ nhiều đường ngangVẽ 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']
3import 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']
4import 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[]
8import 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']
6import 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']
7import 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[]
8import 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[]
9Output:
Để 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[]
5Vẽ 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']
3import 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']
4import 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[]
9Output:
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 đồ
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]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[]
- Xem câu trả lời này cho các đường thẳng đứng với
58import 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[]
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[]
3import 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[]
5Cố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[]
6Trục thời gian
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ặcimport 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[]
64import 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[]
- Sử dụng
65import 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[]
66import 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[]
- Cho
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.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[]
- Sử dụng
- Cột ngày trên trục phải là
70. Nếu sử dụng gấu trúc, thì hãy sử dụngimport 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[]
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
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[]
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[]
0Barplot 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
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 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[]
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 ..