[英]How can I put ax like a plot and no subplot with matplotlib?
this is the code: 这是代码:
#/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
f = lambda x: pow(x, 3) / (pow(x, 2) - x - 6)
fig, ax = plt.subplots()
x = np.linspace(-15.0, 15.0, 1000)
plt.axis([x[0], x[-1], -15.0, 15.0])
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ticks = []
for i in range(-15, 16, 5):
ticks.append(i)
ticks.remove(0)
ax.set_xticks(ticks)
ax.set_yticks(ticks)
plt.plot(x, f(x), color='b', linestyle='-', lw=1.5)
plt.plot(x, x + 1.0, color='b', linestyle='--', lw=1.5)
plt.legend([r'$f(x)=\frac{x^3}{x^2-x-6}$', r'$y=x+1$'], loc='lower right')
plt.title('Discontinuous Function')
plt.grid('on')
plt.show()
I want to set ax like a plot, not like a subplots. 我想像情节一样设定斧头,而不是像子图一样。 How can I change it?
我该如何更改? Thanks.
谢谢。
Why can you not change 为什么你不能改变
plt.plot(x, f(x), color='b', linestyle='-', lw=1.5)
plt.plot(x, x + 1.0, color='b', linestyle='--', lw=1.5)
plt.legend([r'$f(x)=\frac{x^3}{x^2-x-6}$', r'$y=x+1$'], loc='lower right')
plt.title('Discontinuous Function')
plt.grid('on')
to 至
ax.plot(x, f(x), color='b', linestyle='-', lw=1.5)
ax.plot(x, x + 1.0, color='b', linestyle='--', lw=1.5)
ax.legend([r'$f(x)=\frac{x^3}{x^2-x-6}$', r'$y=x+1$'], loc='lower right')
ax.set_title('Discontinuous Function') # notice this one changed to set_title
ax.grid()
plt.show() # keep this one plt.
? ? Otherwise, I'm not quite sure what your question is asking.
否则,我不太确定您的问题在问什么。 Where exactly is the problem?
问题到底在哪里? Do you have a traceback?
你有回溯吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.