简体   繁体   English

在同一图中放置2个x轴刻度

[英]Putting 2 scale for x-axis in the same plot

I've seen examples of how to add 2 scales for y-axis in a single plot, with the command twinx() . 我已经看过如何使用命令twinx()在单个图中为y轴添加2个比例的示例。 However, I still couldn't figure out how to add 2 scales for the x-axis. 但是,我仍然无法弄清楚如何为x轴添加2个刻度。 In my case, I import the [x,y] data from MATLAB and plot them. 在我的例子中,我从MATLAB导入[x,y]数据并绘制它们。 I would like to have my original data of x(we can call it x1 ) displayed at the bottom of the figure, and the normalized x-data(we can call it x2 ) on the top of the figure. 我想在图的底部显示x的原始数据(我们可以称之为x1 ),并在图的顶部显示标准化的x数据(我们可以称之为x2 )。 My code is: 我的代码是:

from scipy.io import loadmat
import matplotlib.pyplot as plt
import numpy as np

# loading data
fid_1 = 'beta_sweep_020.mat'
m1 = loadmat(fid_1)

x = m1['vt_dX']
x1 = np.reshape(x, x.size)
x2 = x1 / (-466.0)
y = m1['vt_beta']
y1 = np.reshape(y, y.size)

# plot
fig = plt.figure()
ax = fig.add_subplot(111)

plt.plot(abs(x1),y1,'r')
plt.show()

Can anyone help me with this? 谁能帮我这个? Thanks. 谢谢。

Use ax.twiny() : 使用ax.twiny()

import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(0,1000000,100)
x2 = x1 / (-466.0)
y1 = np.log(x1)

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
line1, = ax1.plot(x1,y1,'r')
line2, = ax2.plot(x2,y1,'b')

plt.legend((line1, line2), ('red', 'blue'))
plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM