如何解决在 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 举报,一经查实,本站将立刻删除。