gpt4 book ai didi

python - 如何添加其他 x 轴但具有不同的比例和颜色(matplotlib)

转载 作者:行者123 更新时间:2023-12-04 07:15:05 26 4
gpt4 key购买 nike

我有以下情节:
enter image description here
三行中的每一行都属于不同的 x 轴比例。例如,全连接线的 x 轴应介于 0.001 和 0.02 之间; kNN 线的 x 轴的范围应在 2 到 40 之间。我想消除当前的 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)

最佳答案

您应该使用 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()
enter image description here

关于python - 如何添加其他 x 轴但具有不同的比例和颜色(matplotlib),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68816531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com