<small id='3EZLd'></small><noframes id='3EZLd'>

    1. <tfoot id='3EZLd'></tfoot>
      <i id='3EZLd'><tr id='3EZLd'><dt id='3EZLd'><q id='3EZLd'><span id='3EZLd'><b id='3EZLd'><form id='3EZLd'><ins id='3EZLd'></ins><ul id='3EZLd'></ul><sub id='3EZLd'></sub></form><legend id='3EZLd'></legend><bdo id='3EZLd'><pre id='3EZLd'><center id='3EZLd'></center></pre></bdo></b><th id='3EZLd'></th></span></q></dt></tr></i><div id='3EZLd'><tfoot id='3EZLd'></tfoot><dl id='3EZLd'><fieldset id='3EZLd'></fieldset></dl></div>
      <legend id='3EZLd'><style id='3EZLd'><dir id='3EZLd'><q id='3EZLd'></q></dir></style></legend>

      • <bdo id='3EZLd'></bdo><ul id='3EZLd'></ul>
    2. 保存由函数 matplotlib python 产生的绘图

      时间:2023-10-19
      <tfoot id='hsg8S'></tfoot>
            1. <i id='hsg8S'><tr id='hsg8S'><dt id='hsg8S'><q id='hsg8S'><span id='hsg8S'><b id='hsg8S'><form id='hsg8S'><ins id='hsg8S'></ins><ul id='hsg8S'></ul><sub id='hsg8S'></sub></form><legend id='hsg8S'></legend><bdo id='hsg8S'><pre id='hsg8S'><center id='hsg8S'></center></pre></bdo></b><th id='hsg8S'></th></span></q></dt></tr></i><div id='hsg8S'><tfoot id='hsg8S'></tfoot><dl id='hsg8S'><fieldset id='hsg8S'></fieldset></dl></div>

                <bdo id='hsg8S'></bdo><ul id='hsg8S'></ul>

                <small id='hsg8S'></small><noframes id='hsg8S'>

                <legend id='hsg8S'><style id='hsg8S'><dir id='hsg8S'><q id='hsg8S'></q></dir></style></legend>

                  <tbody id='hsg8S'></tbody>

                本文介绍了保存由函数 matplotlib python 产生的绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我创建了一个函数,它从数据集中获取一系列值并输出一个图.例如:

                I created a function that takes a range of values from a data set and outputs a plot. For instance:

                my_plot(location_dataset, min_temperature, max_temperature) 将返回函数中指定温度范围内的降水图.

                my_plot(location_dataset, min_temperature, max_temperature) will return a plot of precipitation for the range of temperature specified in the function.

                假设我想保存加利福尼亚温度在 60-70F 之间的图.因此,我将调用我的函数 my_plot(California, 60, 70) 并在温度介于 60 和 70F 之间时获得加利福尼亚的降水图.

                Let's say I want to save the plot for the temperature between 60-70F in California. so, I would call my function my_plot(California, 60, 70) and will get a plot of precipitation for California when temperatures are between 60 and 70F.

                我的问题是:如何将调用函数产生的绘图保存为 jpeg 格式?

                My question is: how do I save a plot that results from calling a function into a jpeg format?

                我知道 plt.savefig() 不是调用函数的结果,但在我的情况下,我该怎么做?

                I know of plt.savefig() when it is not the result of calling a function but in my case how do I do this?

                谢谢!

                更多细节:这是我的代码(高度简化):

                More details: here is my code (heavily simplified):

                import matplotlib.pyplot as plt
                
                def my_plot(location_dataset, min_temperature, max_temperature):
                    condition = (location_dataset['temperature'] > min_temperature) & (dataset['temperature'] <= max_temperature)
                    subset = location_dataset[condition] # subset the data based on the temperature range
                
                    x = subset['precipitation'] # takes the precipitation column only
                    plt.figure(figsize=(8, 6))
                    plt.plot(x)
                    plt.show()
                

                然后我将这个函数称为如下:my_plot(California, 60, 70) 我得到了 60-70 温度范围的图.如何在函数定义中不包含 savefig 的情况下保存此图(这是因为我需要更改最低和最高温度参数.

                So then I call this function as follow: my_plot(California, 60, 70) and I get my plot for the 60-70 temperature range. how do I save this plot without having the savefig inside the function definition (and that is because I need to change the min and max temperature parameters.

                推荐答案

                figure 的引用指向某个变量,并从函数中返回:

                Take the reference to the figure to some variable, and return it from your function:

                import matplotlib.pyplot as plt
                
                def my_plot(location_dataset, min_temperature, max_temperature):
                    condition = (location_dataset['temperature'] > min_temperature) & (dataset['temperature'] <= max_temperature)
                    subset = location_dataset[condition] # subset the data based on the temperature range
                
                    x = subset['precipitation'] # takes the precipitation column only
                    # N.B. referenca taken to fig
                    fig = plt.figure(figsize=(8, 6))
                    plt.plot(x)
                    plt.show()
                
                    return fig
                

                调用该函数时,可以使用引用保存图形:

                When you call this function, you can use the reference for saving the figure:

                fig = my_plot(...)
                fig.savefig("somefile.png")
                

                这篇关于保存由函数 matplotlib python 产生的绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何使用 Python PIL 将图像保存到特定目录? 下一篇:程序完成后如何将控制台打印到文本文件(Python)?

                相关文章

                  <tfoot id='pJHro'></tfoot>

                  <small id='pJHro'></small><noframes id='pJHro'>

                  • <bdo id='pJHro'></bdo><ul id='pJHro'></ul>
                  1. <legend id='pJHro'><style id='pJHro'><dir id='pJHro'><q id='pJHro'></q></dir></style></legend>

                  2. <i id='pJHro'><tr id='pJHro'><dt id='pJHro'><q id='pJHro'><span id='pJHro'><b id='pJHro'><form id='pJHro'><ins id='pJHro'></ins><ul id='pJHro'></ul><sub id='pJHro'></sub></form><legend id='pJHro'></legend><bdo id='pJHro'><pre id='pJHro'><center id='pJHro'></center></pre></bdo></b><th id='pJHro'></th></span></q></dt></tr></i><div id='pJHro'><tfoot id='pJHro'></tfoot><dl id='pJHro'><fieldset id='pJHro'></fieldset></dl></div>