如何在matplotlib中处理图形

发布于 2024-12-01 08:14:44 字数 1086 浏览 0 评论 0 原文

我是 Python/Django 的新手,我尝试创建一个生成绘图的网站。为了生成绘图,我使用 matplotlib。

当我第一次运行页面时,一切正常,但刷新结果后,我得到错误的图(由新图和旧图组成)。

这里是代码:

    from pylab import *

    ...


    #prepare values and labels
    values = []
    labels = []
    values.append(float(user.playcount)/sum*100)
    labels.append(user.name)

    for friend in friends:
        friend = friend.getInfo()
        values.append(float(friend.playcount)/sum*100)
        labels.append(friend.name)

    # create figure
    figure(1)

    # set some parameters
    params = { 'axes.labelsize': 6, 'text.fontsize': 6, 'font.size': 6, 'legend.fontsize': 6, 'xtick.labelsize': 6, 'ytick.labelsize': 6,}        
    rcParams.update(params)

    # draw, add legend and save
    pie(values, labels=labels, shadow=False)
    l = legend(loc='lower center', ncol= 5, bbox_to_anchor=(0.5, -0.25))
    l.get_frame().set_alpha(0.0)
    savefig('media/images/3.png', dpi=100, transparent=True)

    #close(1)

如果我取消注释 close(1) 指令,刷新页面将会出现问题,因为应用程序将卡在figure() 指令上。

谁能告诉我问题出在哪里?我认为图形处理有问题,但我不知道到底是什么。

I am new new in Python/Django and I try to create a website which generates plots. To generate plots I use matplotlib.

When I run the page first time everything is ok, but after refresh in result I get wrong plot (composed of new and old plot).

Here the code:

    from pylab import *

    ...


    #prepare values and labels
    values = []
    labels = []
    values.append(float(user.playcount)/sum*100)
    labels.append(user.name)

    for friend in friends:
        friend = friend.getInfo()
        values.append(float(friend.playcount)/sum*100)
        labels.append(friend.name)

    # create figure
    figure(1)

    # set some parameters
    params = { 'axes.labelsize': 6, 'text.fontsize': 6, 'font.size': 6, 'legend.fontsize': 6, 'xtick.labelsize': 6, 'ytick.labelsize': 6,}        
    rcParams.update(params)

    # draw, add legend and save
    pie(values, labels=labels, shadow=False)
    l = legend(loc='lower center', ncol= 5, bbox_to_anchor=(0.5, -0.25))
    l.get_frame().set_alpha(0.0)
    savefig('media/images/3.png', dpi=100, transparent=True)

    #close(1)

If I uncomment close(1) instruction there will problem with refresh page because application will get stuck on figure() instruction.

Could anyone tell me where is the problem? I think there is something wrong with figure disposing but I don't have idea what exactly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有心的人 2024-12-08 08:14:44

我认为您会受益于使用 cla() 轴方法,该方法 清除轴。您可以在完成任何操作之前在脚本顶部执行此操作;甚至在第一个情节之前。我会使用它而不是 close(),因为这样第二次该数字将不存在。因此,我会将 figure(1) 行替换为

figure(1)
figure(1).gca().cla()

This 将图形设置为图 1,然后获取当前轴 (gca()),然后使用 < 清除它代码>cla()。这是我独立于 django 运行的一个脚本,它对我有用,并且模拟了我认为你正在尝试做的事情:

from pylab import *

#prepare values and labels
values = [34,17,29,6,14]
labels = ["john","jane","jim","jason","judy"]

# create figure
figure(1)
figure(1).gca().cla()

# set some parameters
params = { 'axes.labelsize': 6, 'text.fontsize': 6, 'font.size': 6, 
           'legend.fontsize': 6, 'xtick.labelsize': 6, 
           'ytick.labelsize': 6,}        
rcParams.update(params)

# draw, add legend and save
pie(values, labels=labels, shadow=False)
l = legend(loc='lower center', ncol= 5, bbox_to_anchor=(0.5, -0.25))
l.get_frame().set_alpha(0.0)
savefig('3.png', dpi=100, transparent=True)


#close(1)

values2 = [24,27,29,16,4]
labels2 = ["dave","donna","derrick","dotty","drew"]

figure(1)
figure(1).gca().cla()

# draw, add legend and save
pie(values2, labels=labels2, shadow=False)
l = legend(loc='lower center', ncol= 5, bbox_to_anchor=(0.5, -0.25))
l.get_frame().set_alpha(0.0)
savefig('4.png', dpi=100, transparent=True)

#show()

你可能只需要一行就可以逃脱;只是让这对我有用:

figure(1).gca().cla()

但也许另一种方式会更清楚。

I think you would benefit from using the cla() axes method, which clears the axes. You can do this at the top of your script before anything is done; even before the first plot. I would use this instead of close(), because then the second time around the figure will not exist. So I would replace the figure(1) line with

figure(1)
figure(1).gca().cla()

This sets the figure to figure 1, and then gets the current axes (gca()) and then clears it with cla(). Here's a script I ran independantly of django, that worked for me and emulates what I think you are trying to do:

from pylab import *

#prepare values and labels
values = [34,17,29,6,14]
labels = ["john","jane","jim","jason","judy"]

# create figure
figure(1)
figure(1).gca().cla()

# set some parameters
params = { 'axes.labelsize': 6, 'text.fontsize': 6, 'font.size': 6, 
           'legend.fontsize': 6, 'xtick.labelsize': 6, 
           'ytick.labelsize': 6,}        
rcParams.update(params)

# draw, add legend and save
pie(values, labels=labels, shadow=False)
l = legend(loc='lower center', ncol= 5, bbox_to_anchor=(0.5, -0.25))
l.get_frame().set_alpha(0.0)
savefig('3.png', dpi=100, transparent=True)


#close(1)

values2 = [24,27,29,16,4]
labels2 = ["dave","donna","derrick","dotty","drew"]

figure(1)
figure(1).gca().cla()

# draw, add legend and save
pie(values2, labels=labels2, shadow=False)
l = legend(loc='lower center', ncol= 5, bbox_to_anchor=(0.5, -0.25))
l.get_frame().set_alpha(0.0)
savefig('4.png', dpi=100, transparent=True)

#show()

You probably can get away with only one line; just having this worked for me:

figure(1).gca().cla()

But it's a bit clearer perhaps the other way.

沉默的熊 2024-12-08 08:14:44

根据 Cristian Ciupitu 对 这个类似问题的回答,使用 figure() 而不是 figure(1) 应该可以解决问题。

According to Cristian Ciupitu's answer to this similar question, use figure() instead of figure(1) should solve the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文