Pyplot用轴移动原点 [英] Pyplot move origin with axis

查看:104
本文介绍了Pyplot用轴移动原点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要创建的绘图代码:

I have some code for a plot I want to create:

import numpy as np
import matplotlib.pyplot as plt

# data
X = np.linspace(-1, 3, num=50, endpoint=True)
b = 2.0
Y = X + b

# plot stuff
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(1, 1, 1)
ax.set_title('linear neuron')

# move axes
ax.spines['left'].set_position(('axes', 0.30))
# ax.spines['left'].set_smart_bounds(True)
ax.yaxis.set_ticks_position('left')

ax.spines['bottom'].set_position(('axes', 0.30))
# ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')

ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# title
title = ax.set_title('Linear Neuron', y=1.10)

# axis ticks
# ax.set_xticklabels([0 if item == 0 else '' for item in X])
# ax.set_yticklabels([])
# for tick in ax.xaxis.get_majorticklabels():
#   tick.set_horizontalalignment('left')
# ax.tick_params(axis=u'both', which=u'both',length=0)

# axis labels
ax.xaxis.set_label_coords(1.04, 0.30 - 0.025)
ax.yaxis.set_label_coords(0.30 - 0.03, 1.04)
y_label = ax.set_ylabel('output')
y_label.set_rotation(0)
ax.set_xlabel('input')

# ax.get_xaxis().set_visible(False)
# ax.get_yaxis().set_visible(False)

# grid
ax.grid(True)


ax.plot(X, Y, '-', linewidth=1.5)

fig.tight_layout()
fig.savefig('plot.pdf')

在此图中,x和y轴已移动.但是,从刻度和刻度标签可以看出,原点并没有随之移动.

In this plot the x and y axis are moved. However, the origin is not moved with then, as one can see from the ticks and ticklabels.

如何始终使用x和y轴移动原点?

How can I always move the origin with the x and y axis?

我想这和简单地查看绘图的另一个区域是一样的,因此x和y轴位于左下角,但不像通常那样位于角上.

I guess it would be the same as simply looking at another area of the plot, so that the x and y axis are at the lower left but not in the corner as they usually are.

要对此进行可视化:

我想要什么:

在箭头指向x和y轴交点的地方,我要有原点(0|0).虚线箭头指向的上方,我希望线向上移动,以便在原点移动时,线在数学上仍处于正确的位置.

Where the arrow points to the x and y axis intersection, I want to have the origin, (0|0). Where the dashed arrow points upwards I want the line to move upwards, so that it is still mathematically at the correct position, when the origin moves.

(工作的最终结果可以在此处) em>

(the final result of the efforts can be found here)

推荐答案

您已经对每件事物进行了很多手动调整,因此该解决方案不是很容易移植.但这是这里:从原始代码中删除ax.spines['bottom'].set_positionax.xaxis.set_label_coords调用,并添加以下代码:

You've done a lot of manual tweaking of where each thing goes, so the solution is not very portable. But here it is: remove the ax.spines['bottom'].set_position and ax.xaxis.set_label_coords calls from your original code, and add this instead:

ax.set_ylim(-1, 6)
ax.spines['bottom'].set_position('zero')
xlabel = ax.xaxis.get_label()
lpos = xlabel.get_position()
xlabel.set_position((1.04, lpos[1]))

仅凭ax.set_ylim即可真正实现提升起源",剩下的就是将标签放置在所需的位置.

The "bring origin up" was really accomplished by just ax.set_ylim, the rest is to get your labels where you want them.

这篇关于Pyplot用轴移动原点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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