简体   繁体   English

如何使用 python 中 matplotlib 中的相同 plt plot 并保存饼图和条形图

[英]How to plot and save pie chart and bar graph using same plt from matplotlib in python

I need to plot and save pie chart and bar graph using same plt from matplotlib in python.我需要 plot 并使用 python 中 matplotlib 中的相同 plt 保存饼图和条形图。 While using plt for both pie chart and bar graph and displaying them in html, second plot is coming as an overlapped figure of first one.在将 plt 用于饼图和条形图并在 html 中显示它们时,第二个 plot 作为第一个的重叠图形出现。 Can you please suggest the method for the same?你能建议同样的方法吗?

Matplotlib isn't stateless, so basically you're always using the same plot. Matplotlib不是无状态的,所以基本上你总是使用相同的 plot。

First things first: spare yourself some hustle.首先要做的事情是:让自己免于忙碌。 Matplotlib is a pain to work with it directly. Matplotlib直接使用它很痛苦。 If you want to get nice plot fast, use Seaborn , which is just aggregator over Matplotlib .如果您想快速获得漂亮的 plot ,请使用Seaborn ,它只是Matplotlib上的聚合器。 As Matplotlib isn't stateless, you need to clean things up, to avoid artifacts.由于Matplotlib不是无状态的,因此您需要清理内容以避免伪影。 I just use wrapper, which I decorate every plotting function with:我只使用包装器,我用以下方式装饰每个绘图 function:

def __disposable_plot(func):
    def wrapper(*args, **kwargs):
        plt.figure(clear=True)
        func(*args, **kwargs)
        plt.close('all')

    return wrapper

If you need kickass plot, use ploty .如果您需要 kickass plot,请使用ploty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM