Skip to main content
 首页 » 编程设计

python之如何在不同比例下操作多个 x 轴使其相互对应

2025年05月04日8insus

我正在尝试制作类似于以下内容的颜色幅度图:

我有三个包含完全相同数量的值的数组。

x1 = B-V(-.5 到 2)

x2 = 温度。 (30,000 到 3000)并且需要是对数刻度

y1 = 幅度(因其他因素而异)

x1、x2 和 y1 数组都是链接在一起的,我想将它们一起绘制成散点图。

我的代码:

#PLOTS 
x1 = bv_array       #B-V 
x2 = t_array        #Temperature 
y1 = Vavg_array     #Magnitude 
fig = plt.figure() 
 
ax1 = fig.add_subplot(111) 
#ax1.xlim(-.5,2) 
ax1.plot(x1, y1,'b--') 
ax2 = ax1.twiny() 
ax2.plot(x2, y1, 'go') 
ax2.set_xlabel('Temperature') 
ax2.invert_xaxis() 
 
#ax2.xscale('log') 
#plt.xscale('log') 
#plt.scatter(x,y) 
 
#plt.scatter(bv_array, Vavg_array, s = 1) 
plt.gca().invert_yaxis() 
 
plt.show() 

请您参考如下方法:

如果我没理解错的话,您应该只能在图表的右侧边缘选择一个轴应该对齐的点,而 matplotlib 将从那里获取它。

ax1 = fig.add_subplot(111) 
 
ax1.xlim(-.5,2) # Set it in axis 1 coords 
 
ax1.plot(x1, y1,'b--') 
ax2 = ax1.twiny() 
ax2.plot(x2, y1, 'go') 
ax2.set_xlabel('Temperature') 
ax2.invert_xaxis() 
 
ax2.xlim(-2, 3) # Set it in axis 2 coords 

要确定该点,计算出哪个轴(线性或对数)将需要沿 x 轴的更多“空间”,将其限制设置为其最大值,然后将其转换为另一个轴的坐标空间以使用它作为它的极限。