我在使用 Matplolib 和 Seaborn 时遇到了一个小问题:基本上,我想要两个不同的 x 轴(具有不同的值),并且我想对齐它们。所以,如你所见:
我希望顶部的 x 轴应该根据主 x 轴的值(按比例)进行“约束”
这是我使用的代码:
numeri = [1000, 10000, 100000, 1000000, 5000000,10000000, 50000000, 100000000, 250000000]
def plot_time():
matplotlib.use('TkAgg')
seaborn.set_context('talk')
plt.title('CPU Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz')
data_df = pd.melt(df_cores_shuffle, 'Num Cores')
ax = sns.lineplot(data=data_df, x='value', y='Num Cores', hue='Num Cores', palette=[
"#1f77b4", "#00FF00", "#00FFFF", "#00BFFF", "#8A2BE2"])
ax.set(xlabel='Time (Seconds)', ylabel='# Cores')
ax.set(yticks=num_cores)
ax2 = ax.twiny()
ax2.set_xticks(numeri)
plt.show()
这是data_df
:
Num Cores variable value
0 2 1000 0.003
1 3 1000 0.002
2 4 1000 0.002
3 5 1000 0.002
4 6 1000 0.002
5 2 10000 0.025
6 3 10000 0.018
7 4 10000 0.014
8 5 10000 0.012
9 6 10000 0.011
10 2 100000 0.316
11 3 100000 0.177
12 4 100000 0.148
13 5 100000 0.144
14 6 100000 0.116
15 2 1000000 3.382
16 3 1000000 2.201
17 4 1000000 1.765
18 5 1000000 1.604
19 6 1000000 1.458
20 2 5000000 19.573
21 3 5000000 13.524
22 4 5000000 10.158
23 5 5000000 8.909
24 6 5000000 8.283
25 2 10000000 40.266
26 3 10000000 25.467
27 4 10000000 20.603
28 5 10000000 18.360
29 6 10000000 17.515
30 2 50000000 221.865
31 3 50000000 138.833
32 4 50000000 111.993
33 5 50000000 98.271
34 6 50000000 93.187
35 2 100000000 462.679
36 3 100000000 298.485
37 4 100000000 238.872
38 5 100000000 211.697
39 6 100000000 198.668
40 2 250000000 1281.431
41 3 250000000 819.359
42 4 250000000 671.026
43 5 250000000 606.328
44 6 250000000 567.414
编辑:对于 JohanC