2017-09-25 80 views
0

我真的很困惑,在Python和IPython的筆記本電腦相同的代碼產生兩個不同的PNG文件與savefigmatplotlib plot在Python中看起來很模糊,在IPython中很鋒利

import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure(figsize=(5,4)) 
ax = fig.add_subplot(1,1,1) 
abc = np.random.uniform(size=(50000,3)) 
print abc.shape 
x = (2*abc[:,0]-abc[:,1]-abc[:,2])/3.0 
y = (abc[:,1]-abc[:,2])/np.sqrt(3) 
ax.plot(x,y,'.',markersize=0.25) 
ax.set_aspect('equal') 
ax.set_xlabel('x') 
ax.set_ylabel('y') 
with open('/tmp/screenshots/foo.png','wb') as f: 
    fig.savefig(f, format='png') 

IPython的筆記本電腦:

enter image description here

的Python:

enter image description here

這是相同的在這兩種情況下,PC都具有相同版本的Python。有沒有辦法使用這兩種方法在IPython中獲取圖像格式? Python版本產生模糊的點,看起來很差。

回答

1

哎呀 - 我想通了,在dpi參數會選擇在兩種情況下以某種方式不同,如果我把它強制dpi=72那麼它看起來不錯:

import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure(figsize=(5,4)) 
ax = fig.add_subplot(1,1,1) 
abc = np.random.uniform(size=(50000,3)) 
print abc.shape 
x = (2*abc[:,0]-abc[:,1]-abc[:,2])/3.0 
y = (abc[:,1]-abc[:,2])/np.sqrt(3) 
ax.plot(x,y,'.',markersize=0.25) 
ax.set_aspect('equal') 
ax.set_xlabel('x') 
ax.set_ylabel('y') 
with open('/tmp/screenshots/foo.png','wb') as f: 
    fig.savefig(f, format='png', dpi=72) 
相關問題