1
votes

I am trying to plot a horizontal line over another plot using matplotlib. Everything works except the title and axis labels never show up. How does this work?

*Edit-sorry, code looks sort of like this: from matplotlib import pyplot as plt n=100

plt.axhline(y=n, label='Old')
plt.plot([5, 6, 7, 8], [100, 110, 115, 150], 'ro', label='New')
plt.xlabel=('Example x')
plt.ylabel=('Example y')
plt.title=('Example Title')
plt.legend()
plt.axis([0,10,50,150])
plt.show()

Everything shows up normally just no title and no axis labels. The legend is there.

1
You'll need to show some code with the observed and expected output. - BrenBarn
plt.xlabel, plt.title, etc. are functions you call, not attributes to be assigned to. - mwaskom
you need to remove the = signs as in: plt.xlabel("Example x"). - MaxNoe

1 Answers

4
votes

Try this:

fig = plt.figure()

ax = fig.add_subplot(111)
ax.axhline(y=n, label='Old')
ax.plot([5, 6, 7, 8], [100, 110, 115, 150], 'ro', label='New')

ax.set_xlabel('Example x')
ax.set_ylabel('Example y')
ax.set_title('Example Title')

ax.legend()
ax.set_xticks([0,10,50,150])
ax.set_yticks([0,10,50,150])

plt.show()