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')
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.
How can I always move the origin with the x and y axis?
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.
To visualize this:
What I want:
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.
(the final result of the efforts can be found here)
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]))
The "bring origin up" was really accomplished by just ax.set_ylim
, the rest is to get your labels where you want them.