【问题标题】:Way to plot a second scaled y axis without explicitly using axes in matplotlib无需在 matplotlib 中显式使用轴即可绘制第二个缩放 y 轴的方法
【发布时间】:2015-07-06 17:55:06
【问题描述】:

这是我目前使用 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},我们只有一个点。但是如果我们看一下y轴,我可以看到左边的y对应于10,但在右边,它对应于1/10=1.0。

【问题讨论】:

  • 你的意思是第二个情节(即子情节)吗?还是您希望它们叠加?

标签: python matplotlib


【解决方案1】:
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()

这将为您提供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()


更新:

由于OP的问题一开始并不清楚,因此将以上两个图作为答案。但由于 OP 要求 duplicate 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()

产生:

【讨论】:

  • 谢谢,很好的答案,但我担心我的问题不清楚。我只需要一对 x,y 数据,但需要另一个 y 轴,这个 y 轴从第一个缩放。
  • 谢谢。这不止是一对数据。例如,如果我绘制:{x=1, y=10},我们只有一个点。但是如果我们看一下 y 轴,我可以看到左边的 y 对应于 10,但在右边,它对应于 1/10=1.0。
  • @Navonod:这只是一个例子!在我的例子中,max(yrep) 是 1.0,max(y) 是 100.0 如果您希望它们都相同,您可以将 yyrep 标准化为 0-1 rangeCheck this answer on how to normalise within a given range
  • 是的,但是你正在绘制两个数据集,我只想要一个数据集,但是两个轴,
  • @Navonod:检查新的更新,如果你仍然认为它不是你想要的,最好开始一个新问题。
【解决方案2】:

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

我不相信解决方案;如果原始页面消失,则编码为:

""" 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
"""

【讨论】:

    【解决方案3】:

    您正在寻找的是 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多