如何将Matplotlib斧头范围设置为正值并镜像到certian值附近 [英] How to set matplotlib ax range positive and mirrored around a certian value

查看:16
本文介绍了如何将Matplotlib斧头范围设置为正值并镜像到certian值附近的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python 2.7下的matplotlib 2.0绘制数据和函数.

I am trying to plot a data and function with matplotlib 2.0 under python 2.7.

函数的 x 值随时间演变,x 首先减小到某个值,然后再增加.

The x values of the function are evolving with time and the x is first decreasing to a certain value, than increasing again.

如果函数是针对时间绘制的,它会显示类似这个数据对时间图的函数

If the function is plotted against time, it shows function like this plot of data against time

我需要相同的x轴演变来绘制实际x值.不幸的是,由于前后两个部分的x值相同,因此两个值混合在一起.这给了我错误的数据图:

I need the same x axis evolution for plotting against real x values. Unfortunately as the x values are the same for both parts before and after, both values are mixed together. This gives me the wrong data plot:

在此示例中,这意味着我需要x轴从值2.4开始并减小至1.0,然后再次增大至2.4.我发誓我之前发现这是可行的,但不幸的是我再也找不到这种痕迹.

In this example it means I need the x-axis to start on value 2.4 and decrease to 1.0 than again increase to 2.4. I swear I found before that this is possible, but unfortunately I can't find a trace about that again.

推荐答案

默认情况下,matplotlib轴呈线性增加.更重要的是,必须有数轴到轴单位的单射映射.所以改变数据范围并不是一个真正的选择(至少当目的是让事情变得简单时).

A matplotlib axis is by default linearly increasing. More importantly, there must be an injective mapping of the number line to the axis units. So changing the data range is not really an option (at least when the aim is to keep things simple).

因此,最好保留原始数字,并仅更改轴上的刻度和刻度标签.例如.您可以使用 FuncFormatter 将原始数字映射到

It would hence be good to keep the original numbers and only change the ticks and ticklabels on the axis. E.g. you could use a FuncFormatter to map the original numbers to

np.abs(x-tp)+tp

其中 tp 将成为转折点.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker

x = np.linspace(-10,20,151)
y = np.exp(-(x-5)**2/19.)

plt.plot(x,y)

tp = 5
fmt = lambda x,pos:"{:g}".format(np.abs(x-tp)+tp)
plt.gca().xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(fmt))

plt.show()

这篇关于如何将Matplotlib斧头范围设置为正值并镜像到certian值附近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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