在 Matplotlib 中设置图表的实际尺寸

如何解决在 Matplotlib 中设置图表的实际尺寸

我需要准确设置图表的尺寸。我试过了,但结果不是我所期望的(如果我设置了 px 和 cm)。上瘾,我想知道如何正确导出图像。感谢您的帮助!

   //Generate Keys
    public static void GenerateRSAKeyPair(out string publicKey,out string privateKey)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048);
        publicKey = rsa.ToXmlString(false);
        privateKey = rsa.ToXmlString(true);
    }

    //Save Keys into files
    private void genKeyPair_Click(object sender,EventArgs e)
    {
        System.Windows.Forms.FolderbrowserDialog fbd = new System.Windows.Forms.FolderbrowserDialog();
        fbd.Description = FYP_WinApp.Properties.Resources.DialogTitle_CreateKey;
        if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string publicKeyPath = System.IO.Path.Combine(fbd.Selectedpath,"publicKey.xml");
            string privateKeyPath = System.IO.Path.Combine(fbd.Selectedpath,"privateKey.xml");

            string publicKey;
            string privateKey;
            GenerateRSAKeyPair(out publicKey,out privateKey);
            using (StreamWriter sw = File.CreateText(publicKeyPath))
            {
                sw.Write(publicKey);
            }

            using (StreamWriter sw = File.CreateText(privateKeyPath))
            {
                sw.Write(privateKey);
            }

        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this,new PropertyChangedEventArgs(propName));
    }

    private string publicKey;
    public string PublicKey
    {
        get
        {
            return this.publicKey;
        }
        set
        {
            if (value != this.publicKey)
            {
                this.publicKey = value;
                Notify("PublicKey");
            }
        }
    }

    //Import public key into a textBox
    private void btmImportPublic_Click(object sender,EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.DefaultExt = ".xml";
        ofd.Filter = FYP_WinApp.Properties.Resources.XML_File_Type;
        ofd.Title = FYP_WinApp.Properties.Resources.DialogTitle_SelectPublicKey;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            using (StreamReader sr = File.OpenText(ofd.FileName))
            {
                this.PublicKey = sr.ReadToEnd();
                rsaBox.Text = File.ReadAllText(ofd.FileName);
            }
        }
    }


    //Encrypt 
    private void btnRSAEncrypt_Click(object sender,EventArgs e)
    {
        RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
        string publicKey = rsaBox.Text;
        string plaintext = PassBox.Text;
        csp.ImportParameters(**publicKey**); (Error unable to conver string to RSAParameters)
        var data = Encoding.Unicode.GetBytes(plaintext);
        var cypher = csp.Encrypt(data,false);
        encryptedText.Text = Convert.ToBase64String(cypher);
    }

预期尺寸:800 像素 x 1000 像素,dpi= 100 我在此处附上导出图像的 Photoshop 屏幕截图 Not correct dimensions!

解决方法

Figure 构造函数接受默认为 80 dpi 的元组(以英寸为单位的数字)。你需要传递一个 dpi 参数来改变这个

from matplotlib.figure import Figure

fig = Figure(figsize=(5,4),dpi=80)

上面是 80dpi 下的 5 英寸 x 4 英寸,即 400 像素 x 320 像素

如果你想要 800 x 1000,你可以做

fig = Figure(figsize=(8,10),dpi=100)

导出图片很简单

fig.savefig("MatPlotLib_Graph.png",dpi = 100)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?