matplotlib绘图点在Python中看起来很模糊,在IPython中则很清晰

2024-04-20 03:44:52 发布

您现在位置:Python中文网/ 问答频道 /正文

这里我真的很困惑;Python和IPython Notebook中的相同代码生成了两个不同的PNG文件savefig

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

在这两种情况下,这是同一台电脑,使用相同版本的Python。有没有一种方法可以使用这两种方法在IPython中获得图像格式?Python版本生成模糊的点,看起来很差。在


Tags: 方法import版本pngasipythonnpfig
1条回答
网友
1楼 · 发布于 2024-04-20 03:44:52

我想出来了,在这两种情况下,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)

相关问题 更多 >