我对VS Code有奇怪的问题。通常我有函数,它允许你在一个图形上画两个函数。(下面的代码)在Pycharm中,我没有任何问题,它显示了我的图。在VS Code中,我有main函数和Jupyter notebook,在那里我导入main并调用函数。没有错误,但它没有显示我的图。
代码:
import numpy as np
import pickle
import matplotlib
import matplotlib.pyplot as plt
import string
import random
def compare_plot(x1:np.ndarray,y1:np.ndarray,x2:np.ndarray,y2:np.ndarray,
xlabel: str,ylabel:str,title:str,label1:str,label2:str):
if x1.shape != y1.shape or min(x1.shape)==0 or x2.shape != y2.shape or min(x2.shape)==0:
return None
else:
plt.plot(x1, y1, 'b', linewidth = 4)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.plot(x2, y2, 'r', linewidth = 2)
plt.title(title)
plt.legend([label1, label2])
plt.show()
x = np.linspace(0, 5, 100)
f = x + 2
g = x**2 - 2 * np.sin(x) + 3
compare_plot(f, g, x, x, 'x', 'y', 'graphic solution', 'f(x)', 'g(x)')提前感谢您的帮助;)
发布于 2020-10-16 03:46:34
你能试试这个吗?
import numpy as np
import pickle
import matplotlib
import matplotlib.pyplot as plt
import string
import random
def compare_plot(x1:np.ndarray,y1:np.ndarray,x2:np.ndarray,y2:np.ndarray,
xlabel: str,ylabel:str,title:str,label1:str,label2:str):
if x1.shape != y1.shape or min(x1.shape)==0 or x2.shape != y2.shape or min(x2.shape)==0:
return None
else:
fig, ax = plt.subplots(figsize=(8, 8))
ax.plot(x1, y1, 'b', linewidth = 4)
ax.plot(x2, y2, 'r', linewidth = 2)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_title(title)
ax.legend([label1, label2])
plt.tight_layout()
plt.show()
x = np.linspace(0, 5, 100)
f = x + 2
g = x**2 - 2 * np.sin(x) + 3
compare_plot(f, g, x, x, 'x', 'y', 'graphic solution', 'f(x)', 'g(x)')https://stackoverflow.com/questions/64378545
复制相似问题