以编程方式在(I)python中制作和保存绘图,而无需先在屏幕上渲染它们

时间:2019-02-26 21:36:16

标签: python graphics ipython spyder

这是一个虚拟脚本,可以绘制三个图并将其保存为PDF。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame({"A":np.random.normal(100),
                   "B":np.random.chisquare(5, size = 100),
                   "C":np.random.gamma(5,size = 100)})

for i in df.columns:
    plt.hist(df[i])
    plt.savefig(i+".pdf", format = "pdf")
    plt.close()

我正在使用使用IPython的spyder。当我运行此脚本时,三个窗口向我弹出然后消失。它可以工作,但是有点烦人。

如何使这些数字保存到pdf而不显示在屏幕上?

我正在寻找类似R的东西

pdf("path/to/plot/name.pdf")
commands
dev.off()

由于没有任何内容显示在屏幕上,但pdf被保存了。

2 个答案:

答案 0 :(得分:2)

啊哈。部分基于重复的建议(并非完全重复),这可行:

// Retrieve order number/s
$select_order = "select * from mytable where customer_email='admin@admin.com' group by customer_order";
$connect_order = mysqli_query($con, $select_order);
while ($row_order=mysqli_fetch_array($connect_order)) {
$customer_order = $row_order['customer_order'];
echo "
<form method='post'>
<input type='submit' value='$customer_order' name='order_current[]'>
</form>
";
}

// Make each grouped order number a separate query
if (isset($_POST['order_current'])) {
$order_all = $_POST['order_current'];
foreach ($order_all as $order_post) {

// Retrieve selected order details
$select_order = "select * from mytable where customer_email='admin@admin.coml' and customer_order='$order_post'";
$connect_order = mysqli_query($con, $select_order);
while ($row_order=mysqli_fetch_array($connect_order)) {
$name = $row_order['customer_name'];
$amount = $row_order['customer_amount'];

// Display selected order details
echo "
$name<br/>
$amount<br/>
";
}
}
}

基本上,将后端设置为类似pdf设备的内容,然后再将其设置回您习惯的位置。

答案 1 :(得分:1)

我指的是这个StackOverflow answer,引用了这个article作为答案。在SO答案中,他们还建议plt.ioff(),但担心如果您需要它可能会禁用其他功能。