matplotlib.pyplot.vlines
so với matplotlib.pyplot.axvline
- Các phương pháp này được áp dụng cho các lô được tạo ra với Seaborn và
pandas.DataFrame.plot
, cả hai đều sử dụng matplotlib
. - Sự khác biệt là
vlines
chấp nhận một hoặc nhiều vị trí cho import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
0, trong khi import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
1 cho phép một vị trí.- Vị trí đơn:
import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
2. - Nhiều vị trí:
import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
3.
vlines
lấy import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
5 và import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
6 làm vị trí trên trục y, trong khi import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
1 lấy import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
5 và import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
6 dưới dạng phần trăm của phạm vi trục y.- Khi chuyển nhiều dòng cho
vlines
, chuyển A import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# used to get the index values [x] for max 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", kind="line"]
# iterate through the axes
for ax in g.axes.flat:
# get y min and max
ymin, ymax = ax.get_ylim[]
# 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].idxmax[]
# add vertical lines
ax.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5]
1 đến import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
5 và import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
6.
- Cũng
import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# used to get the index values [x] for max 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", kind="line"]
# iterate through the axes
for ax in g.axes.flat:
# get y min and max
ymin, ymax = ax.get_ylim[]
# 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].idxmax[]
# add vertical lines
ax.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5]
4 và import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# used to get the index values [x] for max 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", kind="line"]
# iterate through the axes
for ax in g.axes.flat:
# get y min and max
ymin, ymax = ax.get_ylim[]
# 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].idxmax[]
# add vertical lines
ax.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5]
5 cho API hướng đối tượng.- Nếu bạn đang vẽ một con số với một cái gì đó như
import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# used to get the index values [x] for max 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", kind="line"]
# iterate through the axes
for ax in g.axes.flat:
# get y min and max
ymin, ymax = ax.get_ylim[]
# 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].idxmax[]
# add vertical lines
ax.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5]
6, thì hãy thay thế import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# used to get the index values [x] for max 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", kind="line"]
# iterate through the axes
for ax in g.axes.flat:
# get y min and max
ymin, ymax = ax.get_ylim[]
# 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].idxmax[]
# add vertical lines
ax.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5]
7 hoặc import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# used to get the index values [x] for max 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", kind="line"]
# iterate through the axes
for ax in g.axes.flat:
# get y min and max
ymin, ymax = ax.get_ylim[]
# 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].idxmax[]
# add vertical lines
ax.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5]
8 bằng import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# used to get the index values [x] for max 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", kind="line"]
# iterate through the axes
for ax in g.axes.flat:
# get y min and max
ymin, ymax = ax.get_ylim[]
# 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].idxmax[]
# add vertical lines
ax.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5]
9 hoặc import pandas as pd
import seaborn as sns
# load data
tips = sns.load_dataset['tips']
# histogram
ax = tips.plot[kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line']
_ = ax.vlines[x=16.5, ymin=0, ymax=30, 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.vlines[x=[0, 17], ymin=0, ymax=45, colors='r']
0, tương ứng.
- Xem câu trả lời này cho các đường ngang với
import pandas as pd
import seaborn as sns
# load data
tips = sns.load_dataset['tips']
# histogram
ax = tips.plot[kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line']
_ = ax.vlines[x=16.5, ymin=0, ymax=30, 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.vlines[x=[0, 17], ymin=0, ymax=45, colors='r']
1.
import numpy as np
import matplotlib.pyplot as plt
xs = np.linspace[1, 21, 200]
plt.figure[figsize=[10, 7]]
# only one line may be specified; full height
plt.axvline[x=36, color='b', label='axvline - full height']
# only one line may be specified; ymin & ymax specified as a percentage of y-range
plt.axvline[x=36.25, ymin=0.05, ymax=0.95, color='b', label='axvline - % of full height']
# multiple lines all full height
plt.vlines[x=[37, 37.25, 37.5], ymin=0, ymax=len[xs], colors='purple', ls='--', lw=2, label='vline_multiple - full height']
# multiple lines with varying ymin and ymax
plt.vlines[x=[38, 38.25, 38.5], ymin=[0, 25, 75], ymax=[200, 175, 150], colors='teal', ls='--', lw=2, label='vline_multiple - partial height']
# single vline with full ymin and ymax
plt.vlines[x=39, ymin=0, ymax=len[xs], colors='green', ls=':', lw=2, label='vline_single - full height']
# single vline with specific ymin and ymax
plt.vlines[x=39.25, ymin=25, ymax=150, colors='green', ls=':', lw=2, label='vline_single - partial height']
# place the legend outside
plt.legend[bbox_to_anchor=[1.0, 1], loc='upper left']
plt.show[]
Cốt truyện ở cấp độ biển
import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
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 index values [x] for max 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", kind="line"]
# iterate through the axes
for ax in g.axes.flat:
# get y min and max
ymin, ymax = ax.get_ylim[]
# 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].idxmax[]
# add vertical lines
ax.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5]
- Đối với
import pandas as pd
import seaborn as sns
# load data
tips = sns.load_dataset['tips']
# histogram
ax = tips.plot[kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line']
_ = ax.vlines[x=16.5, ymin=0, ymax=30, 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.vlines[x=[0, 17], ymin=0, ymax=45, colors='r']
2 giá trị tối đa của cả hai sự kiện xảy ra tại import pandas as pd
import seaborn as sns
# load data
tips = sns.load_dataset['tips']
# histogram
ax = tips.plot[kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line']
_ = ax.vlines[x=16.5, ymin=0, ymax=30, 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.vlines[x=[0, 17], ymin=0, ymax=45, colors='r']
3.
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 seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
0 dựa trên chỉ mục thanh, không phải nhãn Tick.import pandas as pd
import seaborn as sns
# load data
tips = sns.load_dataset['tips']
# histogram
ax = tips.plot[kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line']
_ = ax.vlines[x=16.5, ymin=0, ymax=30, 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.vlines[x=[0, 17], ymin=0, ymax=45, colors='r']
5 sẽ hiển thị các vị trí và nhãn.
import pandas as pd
import seaborn as sns
# load data
tips = sns.load_dataset['tips']
# histogram
ax = tips.plot[kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line']
_ = ax.vlines[x=16.5, ymin=0, ymax=30, 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.vlines[x=[0, 17], ymin=0, ymax=45, colors='r']
Trục thời gian
- Các ngày trong khung dữ liệu là trục x phải là
import pandas as pd
import seaborn as sns
# load data
tips = sns.load_dataset['tips']
# histogram
ax = tips.plot[kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line']
_ = ax.vlines[x=16.5, ymin=0, ymax=30, 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.vlines[x=[0, 17], ymin=0, ymax=45, colors='r']
6. Nếu cột hoặc chỉ mục không phải là loại chính xác, nó phải được chuyển đổi với import pandas as pd
import seaborn as sns
# load data
tips = sns.load_dataset['tips']
# histogram
ax = tips.plot[kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line']
_ = ax.vlines[x=16.5, ymin=0, ymax=30, 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.vlines[x=[0, 17], ymin=0, ymax=45, colors='r']
7.- Nếu một mảng hoặc danh sách ngày đang được sử dụng, 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, tương ứng.
import seaborn as sns
# sample data
fmri = sns.load_dataset["fmri"]
# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table[index='timepoint', columns='event', values='signal', aggfunc='mean'].idxmax[]
# plot
g = sns.lineplot[data=fmri, x="timepoint", y="signal", hue="event"]
# y min and max
ymin, ymax = g.get_ylim[]
# vertical lines
g.vlines[x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2]
0 sẽ chấp nhận một ngày như import pandas as pd
import seaborn as sns
# load data
tips = sns.load_dataset['tips']
# histogram
ax = tips.plot[kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line']
_ = ax.vlines[x=16.5, ymin=0, ymax=30, 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.vlines[x=[0, 17], ymin=0, ymax=45, colors='r']
9 hoặc import pandas_datareader as web # conda or pip install this; not part of pandas
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
# get test data; this data is downloaded with the Date column in the index as a 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; the index is a datetime index
ax = df.plot[figsize=[9, 6], title='S&P 500', ylabel='Price']
# add vertical lines
ax.vlines[x=[datetime[2020, 9, 2], '2020-09-24'], ymin=3200, ymax=3600, color='r', label='test lines']
ax.legend[bbox_to_anchor=[1, 1], loc='upper left']
plt.show[]
0.
import pandas_datareader as web # conda or pip install this; not part of pandas
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
# get test data; this data is downloaded with the Date column in the index as a 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; the index is a datetime index
ax = df.plot[figsize=[9, 6], title='S&P 500', ylabel='Price']
# add vertical lines
ax.vlines[x=[datetime[2020, 9, 2], '2020-09-24'], ymin=3200, ymax=3600, color='r', label='test lines']
ax.legend[bbox_to_anchor=[1, 1], loc='upper left']
plt.show[]