user2696225
user2696225

Reputation: 379

Positions of spines in matplotlib

I have a plot: enter image description here

but I would like the ticks to be at the zero axes (as in a normal graph...). I have seen this example and also this example but am having issues:

if i try:

import matplotlib.pyplot as plt
import numpy as np

def f(t):
    A = 2409.505
    rho = 0.3260
    C = 0.00
    if C == 0.0:
        return (A*np.exp(-t/rho))
    else:
        return (A*np.exp(-t/rho)) - (C/r**6)

t1 = np.arange(0, 15, 0.01)
plt.xlim(-1, 16)
plt.ylim(-500, 2500)
plt.spines['left'].setposition('0')
plt.spines['bottom'].setposition('0')
plt.plot(t1, f(t1), 'b', linewidth = '1.5')
plt.tick_params(top = 'off', right = 'off')
plt.grid(linestyle = '--', linewidth = 0.05)
plt.show()

i get the error:

Traceback (most recent call last):
  File "C:\Users\Rebecca\Downloads\pyplot_simple.py", line 16, in <module>
    plt.spines['left'].setposition('0')
AttributeError: 'module' object has no attribute 'spines'

and if i try:

import matplotlib.pyplot as plt
import numpy as np

def f(t):
    A = 2409.505
    rho = 0.3260
    C = 0.00
    if C == 0.0:
        return (A*np.exp(-t/rho))
    else:
        return (A*np.exp(-t/rho)) - (C/r**6)

t1 = np.arange(0, 15, 0.01)
plt.xlim(-1, 16)
plt.ylim(-500, 2500)
plt.axis['xzero'].set_axisline_style('-')
plt.axis['xzero'].set_visible(True)
plt.spines['bottom'].setposition('0')
plt.plot(t1, f(t1), 'b', linewidth = '1.5')
plt.tick_params(top = 'off', right = 'off')
plt.grid(linestyle = '--', linewidth = 0.05)
plt.show()

i get the error:

Traceback (most recent call last):
  File "C:\Users\Rebecca\Downloads\pyplot_simple.py", line 16, in <module>
    plt.axis['xzero'].set_axisline_style('-')
TypeError: 'function' object is not subscriptable

I am really confused and I have no doubt I am, once again, missing something really obvious, so any help would be very gratefully received
Cheers

Upvotes: 5

Views: 16760

Answers (1)

M4rtini
M4rtini

Reputation: 13539

try this:

plt.gca().spines['bottom'].set_position(('data',0))

Upvotes: 19

Related Questions