简体   繁体   中英

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() . However, I still couldn't figure out how to add 2 scales for the x-axis. In my case, I import the [x,y] data from MATLAB and plot them. 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. 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() :

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()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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