为了账号安全,请及时绑定邮箱和手机立即绑定

matplotlib:具有不同比例尺的叠加图?

matplotlib:具有不同比例尺的叠加图?

12345678_0001 2019-12-13 09:41:40
到目前为止,我有以下代码:colors = ('k','r','b')ax = []for i in range(3):    ax.append(plt.axes())    plt.plot(datamatrix[:,0],datamatrix[:,i],colors[i]+'o')    ax[i].set(autoscale_on=True)有了autoscale_on=True每个轴的选项,我认为每个图都应该有其自己的y轴限制,但看起来它们都共享相同的值(即使它们共享不同的轴)。如何设置它们的缩放比例以显示每个范围datamatrix[:,i](只是对.set_ylim()?的显式调用),而且如何为datamatrix[:,2]上面可能需要的第三个变量()创建y轴偏移量?谢谢大家
查看完整描述

3 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

在此处输入图片说明


# d = Pandas Dataframe, 

# ys = [ [cols in the same y], [cols in the same y], [cols in the same y], .. ] 

def chart(d,ys):


    from itertools import cycle

    fig, ax = plt.subplots()


    axes = [ax]

    for y in ys[1:]:

        # Twin the x-axis twice to make independent y-axes.

        axes.append(ax.twinx())


    extra_ys =  len(axes[2:])


    # Make some space on the right side for the extra y-axes.

    if extra_ys>0:

        temp = 0.85

        if extra_ys<=2:

            temp = 0.75

        elif extra_ys<=4:

            temp = 0.6

        if extra_ys>5:

            print 'you are being ridiculous'

        fig.subplots_adjust(right=temp)

        right_additive = (0.98-temp)/float(extra_ys)

    # Move the last y-axis spine over to the right by x% of the width of the axes

    i = 1.

    for ax in axes[2:]:

        ax.spines['right'].set_position(('axes', 1.+right_additive*i))

        ax.set_frame_on(True)

        ax.patch.set_visible(False)

        ax.yaxis.set_major_formatter(matplotlib.ticker.OldScalarFormatter())

        i +=1.

    # To make the border of the right-most axis visible, we need to turn the frame

    # on. This hides the other plots, however, so we need to turn its fill off.


    cols = []

    lines = []

    line_styles = cycle(['-','-','-', '--', '-.', ':', '.', ',', 'o', 'v', '^', '<', '>',

               '1', '2', '3', '4', 's', 'p', '*', 'h', 'H', '+', 'x', 'D', 'd', '|', '_'])

    colors = cycle(matplotlib.rcParams['axes.color_cycle'])

    for ax,y in zip(axes,ys):

        ls=line_styles.next()

        if len(y)==1:

            col = y[0]

            cols.append(col)

            color = colors.next()

            lines.append(ax.plot(d[col],linestyle =ls,label = col,color=color))

            ax.set_ylabel(col,color=color)

            #ax.tick_params(axis='y', colors=color)

            ax.spines['right'].set_color(color)

        else:

            for col in y:

                color = colors.next()

                lines.append(ax.plot(d[col],linestyle =ls,label = col,color=color))

                cols.append(col)

            ax.set_ylabel(', '.join(y))

            #ax.tick_params(axis='y')

    axes[0].set_xlabel(d.index.name)

    lns = lines[0]

    for l in lines[1:]:

        lns +=l

    labs = [l.get_label() for l in lns]

    axes[0].legend(lns, labs, loc=0)


    plt.show()


查看完整回答
反对 回复 2019-12-13
  • 3 回答
  • 0 关注
  • 837 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号