将图添加到matplotlib中的给定图形

时间:2018-02-08 13:54:24

标签: python matplotlib

我在代码的一部分中创建了一个数字,如下所示:

n = arange(51)
fig3 = plt.figure()
plt.semilogy(n,a1mag,'ro')

现在,我想在代码的后半部分为此图添加另一个图。有没有办法在绘图时访问fig3?

1 个答案:

答案 0 :(得分:7)

建议完全保留在pyplot状态机中,或者完全保留在面向对象的API中;混合这两者导致头痛。

pyplot

public class UserEntity
{
    public string Name { get; set; }
}

public interface IRepository
{
    UserEntity GetUserEntity();
}

public sealed class Repository : IRepository
{
    public UserEntity GetUserEntity()
    {
        return new UserEntity
        {
            Name = "User's name"
        };
    }
}

public interface IService
{
    UserEntity GetUserEntity();
}

public sealed class Service : IService
{
    private readonly Lazy<IRepository> lazyRepository;

    public Service(Lazy<IRepository> lazyRepository)
    {
        this.lazyRepository = lazyRepository;
    }

    public UserEntity GetUserEntity()
    {
        var user = this.lazyRepository.Value.GetUserEntity();
        return user;
    }
}

class Program
    {
        static void Main(string[] args)
        {
            UnityContainer container = new UnityContainer();

            container.RegisterType<IRepository, Repository>();
            container.RegisterType<IService, Service>();

            IService resolvedService = container.Resolve<IService>();

            Console.WriteLine($"Name: {resolvedService.GetUserEntity().Name}");


            Console.WriteLine("\n\nTap to continue...");
            Console.ReadLine();
        }
    }

面向对象的API

plt.figure(3)
plt.semilogy(x,y,'ro')

# .. do other stuff
# reactivate figure 3
plt.figure(3)
plt.plot(x,z)