绘制第二个缩放y轴的方法,无需在matplotlib中明确使用轴

时间:2015-07-06 17:55:07

标签: python matplotlib

目前我正在使用matplotlib绘图。我想要包括的是标记的第二个轴,它只是y的倒数。我确实看到以前的答案使用了两个轴的形成 - 但是有一种简单的方法可以使用下面更简单的plt方法吗?

 plt.scatter(chart_dict[chart][0], chart_dict[chart][1], c=colours[count], alpha=1.0, label=chart, lw = 0)
 plt.ylabel(y_lbl)
 plt.xlabel(x_lbl)
 plt.yscale('log')

总而言之,我只是想绘制数据,但是对于相同的数据有两个y轴。如果我绘制:{x = 1,y = 10},我们只有一个点。但是如果我们看看yaxis,我可以看到左边的y对应于10,但是在右边,它对应于1/10 = 1.0。

3 个答案:

答案 0 :(得分:3)

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1,10,100)
y = x**2
yrep = np.reciprocal(y)

如果你想要重叠的地块,你可以做

plt.scatter(x,y,label='x vs y')
plt.scatter(x,yrep,label='x vs reciprocal(y)')
plt.legend(loc='best')
plt.show()

enter image description here

这将为您提供一个包含xy and yrep

的图表

如果你想要将它们并排放在两个地块中,你可以这样做:

fig = plt.figure()
ax = fig.add_subplot(121)
ax.scatter(x,y,label='x vs y')
ax.legend(loc='best')
ax2 = fig.add_subplot(122)
ax2.scatter(x,yrep,label='x vs reciprocal(y)')
ax2.legend(loc='best')
fig.show()

enter image description here

<强>更新

由于OP的问题首先不明确,所以上面的两个图显示为答案。但由于OP要求 重复yaxis ,以下是代码的更新版本:

fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x,y,label='x vs y',color='red')
ax.legend(loc='best')
ax2 = ax.twinx() # HERE WE USE twinx() to get a scaled yaxis
ax2.scatter(x,y,alpha=0.001) # they don't appear on the plot
ax2.set_ylabel('yreplica')
fig.show()

产生: enter image description here

答案 1 :(得分:0)

您是否正在尝试创建类似Arrhenius图的东西,在同一轴上有两个刻度,y和(1 / y)?您可能需要查看http://hsugawa.blogspot.com/2010/01/matplotlib-arrhenius-plot.html

Arrhenius plot example

我不赞成这个解决方案;如果原始页面消失,编码为:

""" A function, v(T)=exp(-A/(kB*T)), is displayed as a straight line
on Arrhenius plot where the logarithm of v(T) is plotted
against reciprocal temperature, 1/T.
Some temperatures are manually ticked at the top axis
in the same way illustrated by
axes_grid example code: simple_axisline4.py
http://matplotlib.sourceforge.net/examples/axes_grid/simple_axisline4.html
"""

from scipy.constants import physical_constants
kB=physical_constants['Boltzmann constant in eV/K'][0]

import numpy as np
arange=np.arange
exp=np.exp

tt=arange(18.,501.)
vv=exp(-0.02/(kB*tt))

import matplotlib.pylab as plt
from mpl_toolkits.axes_grid.parasite_axes import SubplotHost

fig=plt.figure(1)
ax1=SubplotHost(fig, 111)
fig.add_subplot(ax1)

ax1.plot(1./tt,vv)
ax1.set_yscale('log')
ax1.set_xlabel('Reciprocal temperature (1/K)')

ax2=ax1.twin() # ax2 is responsible for "top" axis and "right" axis
tticks=np.array([20.,30.,50.,100.,300.])
ax2.set_xticks( [ 1/t for t in tticks ] )
ax2.set_xticklabels(tticks)
ax2.axis["top"].label.set_visible(True)
ax2.set_xlabel('Temperature (K)')
ax2.set_yticks([])

plt.show()

"""
Python 2.5.4 |EPD 5.1.1| (r254:67916, Sep 25 2009, 12:11:02) [MSC v.1310 32 bit (Intel)] on win32
matplotlib 0.99.1.1
"""

答案 2 :(得分:0)

您要查找的是ax.secondary_x / yaxis提示。

下面是波数到波长的示例:

ax.plot(x,y,label="data")
# Define Function and reciproce function
def w2L(x): #cm-1 in nm
   return 10**7/x
def L2w(x): #nm in cm-1
   return 10**7/x

# Create secondary axis
ax2 = ax.secondary_xaxis('top', functions=(w2L,L2w))
ax2.set_xlabel(r'$\lambda$ / nm') #Name

https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/secondary_axis.html