两个对应的yaxis

2024-05-23 13:51:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用两个对应的y轴绘制一个图。我想要的是在左y轴上有一个单位的值(例如米),在左y轴上用另一个单位(例如英寸)来表示相应的值。 我设法用米和英寸来做这个。在

然而,当转换不如乘以某个值那么明显时,事情就不那么容易了。 在我的例子中,我试图绘制通量和震级。从通量到震级的公式是: 23.9-对数(通量)。在

当我画出米和英寸时起作用的东西已经不起作用了。 这是我的代码:

host = host_subplot(111)
host.set_xlim((0.1,5)) 
host.set_ylim((1.,50.))

par1 = host.twinx()
par1.set_ylim((23.899999999999999, 19.652574989159952)) # Converting the ylim from flux to mag

host.set_xlabel('Wavelength (Microns)')
host.set_ylabel('Flux density ($\mu$Jy)')
par1.set_ylabel("Magnitude ")

host.errorbar(x, flux, fmt='rx' , xerr=errx, yerr = errorFlux)
par1.errorbar(x, mag, fmt='bx', xerr=errx, yerr=errorMag)

如果这是可行的,两个图应该重叠,但他们没有(再次,我做这个工作时,做了类似的事情从米到英寸)。我怀疑这与日志有关,但当设置刻度为日志时,情况更糟。在


Tags: host绘制单位事情通量set英寸fmt
2条回答

所以我最终在matplotlib中创建了一个新的比例。它可以改进,但下面是我的类定义,基于http://matplotlib.org/examples/api/custom_scale_example.html

import numpy as np
import matplotlib.pyplot as plt

from matplotlib import scale as mscale
from matplotlib import transforms as mtransforms

class MagScale(mscale.ScaleBase):
    name = 'mag'

    def __init__(self, axis, **kwargs):
        mscale.ScaleBase.__init__(self)
        self.thresh = None #thresh

    def get_transform(self):
        return self.MagTransform(self.thresh)

    def set_default_locators_and_formatters(self, axis):
        pass

    class MagTransform(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True

        def __init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        def transform_non_affine(self, mag):
            return 10**((np.array(mag) -1)/(-2.5))

        def inverted(self):
            return MagScale.InvertedMagTransform(self.thresh)

    class InvertedMagTransform(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True

        def __init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        def transform_non_affine(self, flux):
            return -2.5 * np.log10(np.array(flux)) + 1.

        def inverted(self):
            return MagScale.MagTransform(self.thresh)



def flux_to_mag(flux):
    return  -2.5 * np.log10(flux) + 1


mscale.register_scale(MagScale)

下面是一个有效的例子:

^{pr2}$

试试这个,取自:http://matplotlib.org/examples/api/two_scales.html 我把我的例子放在下面。在

我只是建立了一些指数增长的数据集。当然,可以更精确地调整坐标轴,比如创建自己的刻度、范围和刻度线;您所要做的就是花一些时间在网上做示例并阅读API文档。另外,我将为您指出这个参考资料http://nbviewer.ipython.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb中的一些好例子。在

from numpy import *
import matplotlib.pyplot as plt


x_data = arange(0,100,.1)
y1_data = x_data**10
y2_data = x_data**4


fig, ax1 = plt.subplots(nrows=1, ncols=1)
ax1.set_yscale('linear')
ax1.plot(x_data,y1_data,'b-')
ax1.set_xlabel("Unitless X's")

ax1.set_ylabel('Linear Scale', color='b')
for tl in ax1.get_yticklabels():
    tl.set_color('b')

ax2 = ax1.twinx()
ax2.plot(x_data,y2_data,'r.')
ax2.set_ylabel("Log Scale", color='r')
ax2.set_yscale('log')
for tl in ax2.get_yticklabels():
    tl.set_color('r')
plt.show()

相关问题 更多 >