matplotlib图形不显示在html代码中的pyscript标记内

v2g6jxz6  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(150)

任何人都可以给予我如何运行和显示我的matplotlib图在html页面使用pyscript的细节,请提到所有的安装所需的,因为我试图打印文本它的工作,但使用matplotlib在python中生成的图是不得到显示on running code inside tag

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>

    <py-env>
        -matplotlib
    </py-env>
</head>
<body>
    <h1>Matplotlib</h1>
    <div id="lineplot"></div>
    <py-script output="lineplot">
        import matplotlib.pyplot as plt
        fig, ax = plt.subplots()

        year_1 = [2016, 2017, 2018, 2019, 2020, 2021]
        population_1 = [42, 43, 45, 47, 48, 50]

        year_2 = [2016, 2017, 2018, 2019, 2020, 2021]
        population_2 = [43, 43, 44, 44, 45, 45]

        plt.plot(year_1, population_1, marker='o', linestyle='--', color='g', label='Country 1')
        plt.plot(year_2, population_2,  marker='d', linestyle='-', color='r', label='Country 2')

        plt.xlabel('Year')
        plt.ylabel('Population (M)')
        plt.title('Year vs Population')
        plt.legend(loc='lower right')

        fig
    </py-script>
</body>
</html>
lg40wkob

lg40wkob1#

**Pyscript的变化非常快。**正如Jeff Glass所指出的here

“不再是alpha版本,尽管每次发布都会有很大的变化。”
你必须努力跟上它。你发布的内容使用过时的语法。
我推荐Jeff Glass from where I quoted above for getting started的一个最近的资源:
video on youtube

他们有一个Matplotlib示例,你可以修改

对你来说很重要的是,在the Pyscript demos page上列出的例子中有一个提供的Matplotlib例子。它可以在there的“MIME渲染”部分找到目前,该部分位于“基本例子”下面和“JS交互”部分上面:

  • 链接到'Matplotlib' pyscript示例运行在web上:here
  • 链接到Github上的'Matplotlib' pyscript示例代码:here

(我注意到,在'Getting started with PyScript' page上,还有一个更精简的matplotlib示例,您可以采用。
让你的代码适应当前的Pyscript Matplotlib示例:

  • 查看在web上运行的适配版本here
  • 修改后的代码:
<html>
 <!-- dcoument based on:
- https://pyscript.net/examples/matplotlib.html and adapting to https://stackoverflow.com/q/76014723/8508004

-->
    <head>
        <title>Matplotlib - from https://pyscript.net/examples/matplotlib.html April 2023</title>
        <meta charset="utf-8" />
        <link rel="icon" type="image/x-icon" href="https://pyscript.net/examples/favicon.png" />
        <link
            rel="stylesheet"
            href="https://pyscript.net/latest/pyscript.css"
        />
        <script defer src="https://pyscript.net/latest/pyscript.js"></script>
        <link rel="stylesheet" href="https://pyscript.net/examples/assets/css/examples.css" />
    </head>
    <body>
        <nav class="navbar" style="background-color: #000000">
            <div class="app-header">
                <a href="/">
                    <img src="https://pyscript.net/examples/logo.png" class="logo" />
                </a>
                <a class="title" href="" style="color: #f0ab3c">Matplotlib</a>
            </div>
        </nav>
        <section class="pyscript">
            <div id="mpl"></div>

            <py-tutor>
                <py-config>
                    packages = [
                      "matplotlib"
                    ]
                    plugins = [
                      "https://pyscript.net/latest/plugins/python/py_tutor.py"
                    ]
                </py-config>

                <py-script>
                    import matplotlib.pyplot as plt
                    fig, ax = plt.subplots()
                    
                    year_1 = [2016, 2017, 2018, 2019, 2020, 2021]
                    population_1 = [42, 43, 45, 47, 48, 50]
                    
                    year_2 = [2016, 2017, 2018, 2019, 2020, 2021]
                    population_2 = [43, 43, 44, 44, 45, 45]
                    
                    plt.plot(year_1, population_1, marker='o', linestyle='--', color='g', label='Country 1')
                    plt.plot(year_2, population_2,  marker='d', linestyle='-', color='r', label='Country 2')
                    
                    plt.xlabel('Year')
                    plt.ylabel('Population (M)')
                    plt.title('Year vs Population')
                    plt.legend(loc='lower right')

                    display(fig, target="mpl")
                </py-script>
            </py-tutor>
        </section>
    </body>
</html>

你可以点击“运行代码段”按钮来运行它;我发现我需要点击“全页”视图选项,然后会出现在右侧,以查看情节。您可以通过点击“关闭”从全页视图右上角回到帖子。)

相关问题