在图表中创建断轴
Create broken axis in graph
我写了下面的代码来做图:
data = pd.read_excel('C:/Users/User/Desktop/Grafiek.xlsx')
fig, ax = plt.subplots()
ax.plot('Datum', 'Percentage', data=data)
fmt_half_year = mdates.MonthLocator(interval=6)
ax.xaxis.set_major_locator(fmt_half_year)
fmt_month = mdates.MonthLocator()
ax.xaxis.set_minor_locator(fmt_month)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
ax.format_xdata = mdates.DateFormatter('%Y-%m')
ax.grid(True)
plt.xlabel('Datum')
plt.ylabel('Percentage')
plt.title('Percentage Trained')
fig.autofmt_xdate()
plt.show()
它给我展示了下图
我想做一个2015-03到2018-03的断轴。有人知道如何编写正确的代码吗?
谢谢!
大量借鉴示例here,我们可以稍微修改它以移动到断开的 x 轴而不是 y 轴。
从那个例子到这里的主要变化:
- 改为
sharey=True
- 关闭 left/right spines 而不是 bottom/top
- 更改 position/angle 对角线以显示断轴
- 改变两个轴的比例,因为左边的轴跨度更短
注意最后一点,这里我将它们设为 1:10 比率,因为我将第二个轴跨度为 40 个月,第一个轴跨度为 space 4 个月。您可能需要稍微不同的比率,因此需要相应地调整每个轴的 xlim
以及 width_ratios
参数。
import matplotlib.dates as mdates
import pandas as pd
import numpy as np
drange = pd.date_range('2015-01-01', periods=78, freq='M')
data = pd.DataFrame({'Datum': drange, 'Percentage': np.random.randn(len(drange))})
fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True, gridspec_kw={'width_ratios': (1, 10)})
fig.subplots_adjust(wspace=0.05)
ax1.plot('Datum', 'Percentage', data=data)
ax2.plot('Datum', 'Percentage', data=data)
fmt_half_year1 = mdates.MonthLocator(interval=6)
fmt_half_year2 = mdates.MonthLocator(interval=6)
ax1.xaxis.set_major_locator(fmt_half_year1)
ax2.xaxis.set_major_locator(fmt_half_year2)
fmt_month1 = mdates.MonthLocator()
fmt_month2 = mdates.MonthLocator()
ax1.xaxis.set_minor_locator(fmt_month1)
ax2.xaxis.set_minor_locator(fmt_month2)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
ax1.format_xdata = mdates.DateFormatter('%Y-%m')
ax2.format_xdata = mdates.DateFormatter('%Y-%m')
ax1.grid(True)
ax2.grid(True)
# hide the spines between ax1 and ax2
ax1.spines.right.set_visible(False)
ax2.spines.left.set_visible(False)
ax2.yaxis.tick_right()
ax2.tick_params(labelright=False) # don't put tick labels at the top
ax1.xaxis.tick_bottom()
datemin1 = np.datetime64(data['Datum'][0], 'M')
datemax1 = np.datetime64(data['Datum'][4], 'M')
ax1.set_xlim(datemin1, datemax1)
datemin2 = np.datetime64(data['Datum'][37], 'M')
datemax2 = np.datetime64(data['Datum'][77], 'M')
ax2.set_xlim(datemin2, datemax2)
fig.text(s='Datum', x=0.5, y=0.05)
ax1.set_ylabel('Percentage')
fig.suptitle('Percentage Trained')
fig.autofmt_xdate()
d = 2. # proportion of vertical to horizontal extent of the slanted line
kwargs = dict(marker=[(-1, -d), (1, d)], markersize=12,
linestyle="none", color='k', mec='k', mew=1, clip_on=False)
ax1.plot([1, 1], [0, 1], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 0], [0, 1], transform=ax2.transAxes, **kwargs)
plt.show()
我写了下面的代码来做图:
data = pd.read_excel('C:/Users/User/Desktop/Grafiek.xlsx')
fig, ax = plt.subplots()
ax.plot('Datum', 'Percentage', data=data)
fmt_half_year = mdates.MonthLocator(interval=6)
ax.xaxis.set_major_locator(fmt_half_year)
fmt_month = mdates.MonthLocator()
ax.xaxis.set_minor_locator(fmt_month)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
ax.format_xdata = mdates.DateFormatter('%Y-%m')
ax.grid(True)
plt.xlabel('Datum')
plt.ylabel('Percentage')
plt.title('Percentage Trained')
fig.autofmt_xdate()
plt.show()
它给我展示了下图
我想做一个2015-03到2018-03的断轴。有人知道如何编写正确的代码吗?
谢谢!
大量借鉴示例here,我们可以稍微修改它以移动到断开的 x 轴而不是 y 轴。
从那个例子到这里的主要变化:
- 改为
sharey=True
- 关闭 left/right spines 而不是 bottom/top
- 更改 position/angle 对角线以显示断轴
- 改变两个轴的比例,因为左边的轴跨度更短
注意最后一点,这里我将它们设为 1:10 比率,因为我将第二个轴跨度为 40 个月,第一个轴跨度为 space 4 个月。您可能需要稍微不同的比率,因此需要相应地调整每个轴的 xlim
以及 width_ratios
参数。
import matplotlib.dates as mdates
import pandas as pd
import numpy as np
drange = pd.date_range('2015-01-01', periods=78, freq='M')
data = pd.DataFrame({'Datum': drange, 'Percentage': np.random.randn(len(drange))})
fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True, gridspec_kw={'width_ratios': (1, 10)})
fig.subplots_adjust(wspace=0.05)
ax1.plot('Datum', 'Percentage', data=data)
ax2.plot('Datum', 'Percentage', data=data)
fmt_half_year1 = mdates.MonthLocator(interval=6)
fmt_half_year2 = mdates.MonthLocator(interval=6)
ax1.xaxis.set_major_locator(fmt_half_year1)
ax2.xaxis.set_major_locator(fmt_half_year2)
fmt_month1 = mdates.MonthLocator()
fmt_month2 = mdates.MonthLocator()
ax1.xaxis.set_minor_locator(fmt_month1)
ax2.xaxis.set_minor_locator(fmt_month2)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
ax1.format_xdata = mdates.DateFormatter('%Y-%m')
ax2.format_xdata = mdates.DateFormatter('%Y-%m')
ax1.grid(True)
ax2.grid(True)
# hide the spines between ax1 and ax2
ax1.spines.right.set_visible(False)
ax2.spines.left.set_visible(False)
ax2.yaxis.tick_right()
ax2.tick_params(labelright=False) # don't put tick labels at the top
ax1.xaxis.tick_bottom()
datemin1 = np.datetime64(data['Datum'][0], 'M')
datemax1 = np.datetime64(data['Datum'][4], 'M')
ax1.set_xlim(datemin1, datemax1)
datemin2 = np.datetime64(data['Datum'][37], 'M')
datemax2 = np.datetime64(data['Datum'][77], 'M')
ax2.set_xlim(datemin2, datemax2)
fig.text(s='Datum', x=0.5, y=0.05)
ax1.set_ylabel('Percentage')
fig.suptitle('Percentage Trained')
fig.autofmt_xdate()
d = 2. # proportion of vertical to horizontal extent of the slanted line
kwargs = dict(marker=[(-1, -d), (1, d)], markersize=12,
linestyle="none", color='k', mec='k', mew=1, clip_on=False)
ax1.plot([1, 1], [0, 1], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 0], [0, 1], transform=ax2.transAxes, **kwargs)
plt.show()