Matplotlib:轴负侧的比例不同 [英] Matplotlib: different scale on negative side of the axis

查看:61
本文介绍了Matplotlib:轴负侧的比例不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

<小时>

我试图在一个绘图上显示三个变量.我已经根据其他一些变量使用不同颜色的线连接了三个点.在此处显示

<小时>

问题

<小时>

我想要做的是在负 x 轴上有一个不同的比例.这将帮助我提供正 x_ticks、不同的轴标签以及图像左侧线条的清晰和整洁表示

<小时>

问题

<小时>
  • 如何从0到负方向设置不同的正x轴?
  • 根据沿该方向绘制的数据进行 xticks
  • 为此新轴具有单独的xlabel
<小时>

其他信息

<小时>

我已经检查了有关包含多个轴的其他问题,例如

关于需要什么 xticks 和标签的问题不是很清楚,所以我暂时忽略了这一点.

Background


I am trying to show three variables on a single plot. I have connected the three points using lines of different colours based on some other variables. This is shown here


Problem


What I want to do is to have a different scale on the negative x-axis. This would help me in providing positive x_ticks, different axis label and also clear and uncluttered representation of the lines on left side of the image


Question


  • How to have a different positive x-axis starting from 0 towards negative direction?
  • Have xticks based on data plotted in that direction
  • Have a separate xlabel for this new axis

Additional information


I have checked other questions regarding inclusion of multiple axes e.g. this and this. However, these questions did not serve the purpose.

Code Used

font_size = 20
plt.rcParams.update({'font.size': font_size})

fig = plt.figure()
ax = fig.add_subplot(111)
#read my_data from file or create it

for case in my_data:

    #Iterating over my_data

    if condition1 == True:
        local_linestyle = '-'
        local_color = 'r'
        local_line_alpha = 0.6
    elif condition2 == 1:
        local_linestyle = '-'
        local_color = 'b'
        local_line_alpha = 0.6
    else:
        local_linestyle = '--'
        local_color = 'g'
        local_line_alpha = 0.6

    datapoint = [case[0], case[1], case[2]]

    plt.plot(datapoint[0], 0, color=local_color)
    plt.plot(-datapoint[2], 0, color=local_color)
    plt.plot(0, datapoint[1], color=local_color)
    plt.plot([datapoint[0], 0], [0, datapoint[1]], linestyle=local_linestyle, color=local_color)
    plt.plot([-datapoint[2], 0], [0, datapoint[1]], linestyle=local_linestyle, color=local_color)
plt.show()
exit()

解决方案

You can define a custom scale, where values below zero are scaled differently than those above zero.

import numpy as np
from matplotlib import scale as mscale
from matplotlib import transforms as mtransforms
from matplotlib.ticker import FuncFormatter

class AsymScale(mscale.ScaleBase):
    name = 'asym'

    def __init__(self, axis, **kwargs):
        mscale.ScaleBase.__init__(self)
        self.a = kwargs.get("a", 1)

    def get_transform(self):
        return self.AsymTrans(self.a)

    def set_default_locators_and_formatters(self, axis):
        # possibly, set a different locator and formatter here.
        fmt = lambda x,pos: "{}".format(np.abs(x))
        axis.set_major_formatter(FuncFormatter(fmt))

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

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

        def transform_non_affine(self, x):
            return (x >= 0)*x + (x < 0)*x*self.a

        def inverted(self):
            return AsymScale.InvertedAsymTrans(self.a)

    class InvertedAsymTrans(AsymTrans):

        def transform_non_affine(self, x):
            return (x >= 0)*x + (x < 0)*x/self.a
        def inverted(self):
            return AsymScale.AsymTrans(self.a)

Using this you would provide a scale parameter a that scales the negative part of the axes.

# Now that the Scale class has been defined, it must be registered so
# that ``matplotlib`` can find it.
mscale.register_scale(AsymScale)

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

ax.plot([-2, 0, 5], [0,1,0])
ax.set_xscale("asym", a=2)

ax.annotate("negative axis", xy=(.25,0), xytext=(0,-30), 
            xycoords = "axes fraction", textcoords="offset points", ha="center")
ax.annotate("positive axis", xy=(.75,0), xytext=(0,-30), 
            xycoords = "axes fraction", textcoords="offset points", ha="center")
plt.show()

The question is not very clear about what xticks and labels are desired, so I left that out for now.

这篇关于Matplotlib:轴负侧的比例不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆