首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VS Code matplot

VS Code matplot
EN

Stack Overflow用户
提问于 2020-10-16 03:35:09
回答 1查看 27关注 0票数 0

我对VS Code有奇怪的问题。通常我有函数,它允许你在一个图形上画两个函数。(下面的代码)在Pycharm中,我没有任何问题,它显示了我的图。在VS Code中,我有main函数和Jupyter notebook,在那里我导入main并调用函数。没有错误,但它没有显示我的图。

代码:

代码语言:javascript
复制
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)')

提前感谢您的帮助;)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-16 03:46:34

你能试试这个吗?

代码语言:javascript
复制
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)')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64378545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档