matplotlib 如何添加额外的x轴,但具有不同的比例和颜色

0aydgbwb  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(104)

我有以下情节:
x1c 0d1x的数据
三条线中的每条线都属于不同的x轴比例。例如,全连接线的x轴范围应在0.001和0.02之间; kNN线的x轴范围应在2和40之间。我想消除当前x轴,并有三个x轴,一个在另一个下面,每个x轴的比例和颜色都不同。
下面是我的代码:

## Plot means
x_full = np.linspace(0.001, 0.02, 20)
x_enn = np.linspace(0.05, 1.95, 20)
x_knn = np.linspace(2, 40, 20)
x = np.arange(len(x_full))

fig, ax = plt.subplots(1, 2, figsize=(13.2, 4))

## Set color
ax[0].set_prop_cycle(color=color_list)
ax[1].set_prop_cycle(color=color_list)

## Plot means
ax[0].plot(x, two_moons_acc_mean['full'], label='Fully Connected')
ax[0].plot(x[1:], two_moons_acc_mean['enn'][0.1:], label=r'$\epsilon$-N')
ax[0].plot(x, two_moons_acc_mean['knn'], label=r'$k$NN')

ax[1].plot(x, two_moons_acc_mean['full'], label='Fully Connected')
ax[1].plot(x[1:], two_moons_acc_mean['enn'][0.1:], label=r'$\epsilon$-N')
ax[1].plot(x, two_moons_acc_mean['knn'], label=r'$k$NN')

## Plot standard deviations
ax[0].fill_between(
    x,
    two_moons_acc_mean['full'] - two_moons_acc_std['full'],
    two_moons_acc_mean['full'] + two_moons_acc_std['full'],
    alpha=0.2
)
ax[0].fill_between(
    x[1:],
    two_moons_acc_mean['enn'][0.1:] - two_moons_acc_std['enn'][0.1:],
    two_moons_acc_mean['enn'][0.1:] + two_moons_acc_std['enn'][0.1:],
    alpha=0.2
)
ax[0].fill_between(
    x,
    two_moons_acc_mean['knn'] - two_moons_acc_std['knn'],
    two_moons_acc_mean['knn'] + two_moons_acc_std['knn'],
    alpha=0.2
)

ax[1].fill_between(
    x,
    two_moons_acc_mean['full'] - two_moons_acc_std['full'],
    two_moons_acc_mean['full'] + two_moons_acc_std['full'],
    alpha=0.2
)
ax[1].fill_between(
    x[1:],
    two_moons_acc_mean['enn'][0.1:] - two_moons_acc_std['enn'][0.1:],
    two_moons_acc_mean['enn'][0.1:] + two_moons_acc_std['enn'][0.1:],
    alpha=0.2
)
ax[1].fill_between(
    x,
    two_moons_acc_mean['knn'] - two_moons_acc_std['knn'],
    two_moons_acc_mean['knn'] + two_moons_acc_std['knn'],
    alpha=0.2
)

## Loglog plot
ax[1].set_xscale('log')
ax[1].set_yscale('log')

## Add Legend
ax[0].legend(loc='lower left', ncol=3, frameon=False)
ax[1].legend(loc='lower left', ncol=3, frameon=False)

字符串

jhiyze9q

jhiyze9q1#

您应该使用3个不同的轴,每个轴对应您需要绘制的每一条线。
第一个可以是:

fig, ax_full = plt.subplots()

full = ax_full.plot(x_full, y_full, color = 'red', label = 'full')

字符串
然后,您可以使用以下命令生成其他命令:

ax_enn = ax_full.twiny()


并在相应的轴上绘制每条线:

enn = ax_enn.plot(x_enn, y_enn, color = 'blue', label = 'enn')


然后,您可以使用以下命令将轴移动到底部:

ax_enn.xaxis.set_ticks_position('bottom')
ax_enn.xaxis.set_label_position('bottom')
ax_enn.spines['bottom'].set_position(('axes', -0.15))


最后定制颜色:

ax_enn.spines['bottom'].set_color('blue')
ax_enn.tick_params(axis='x', colors='blue')
ax_enn.xaxis.label.set_color('blue')

完整代码

import numpy as np
import matplotlib.pyplot as plt

x_full = np.linspace(0.001, 0.02, 20)
x_enn = np.linspace(0.05, 1.95, 20)
x_knn = np.linspace(2, 40, 20)

y_full = np.random.rand(len(x_full))
y_enn = np.random.rand(len(x_enn))
y_knn = np.random.rand(len(x_knn))

fig, ax_full = plt.subplots()

full = ax_full.plot(x_full, y_full, color = 'red', label = 'full')
ax_full.spines['bottom'].set_color('red')
ax_full.tick_params(axis='x', colors='red')
ax_full.xaxis.label.set_color('red')

ax_enn = ax_full.twiny()
enn = ax_enn.plot(x_enn, y_enn, color = 'blue', label = 'enn')
ax_enn.xaxis.set_ticks_position('bottom')
ax_enn.xaxis.set_label_position('bottom')
ax_enn.spines['bottom'].set_position(('axes', -0.15))
ax_enn.spines['bottom'].set_color('blue')
ax_enn.tick_params(axis='x', colors='blue')
ax_enn.xaxis.label.set_color('blue')

ax_knn = ax_full.twiny()
knn = ax_knn.plot(x_knn, y_knn, color = 'green', label = 'knn')
ax_knn.xaxis.set_ticks_position('bottom')
ax_knn.xaxis.set_label_position('bottom')
ax_knn.spines['bottom'].set_position(('axes', -0.3))
ax_knn.spines['bottom'].set_color('green')
ax_knn.tick_params(axis='x', colors='green')
ax_knn.xaxis.label.set_color('green')

lines = full + enn + knn
labels = [l.get_label() for l in lines]
ax_full.legend(lines, labels)

plt.tight_layout()

plt.show()


的数据

相关问题