使用 matplotlib 绘制亚像素精度散点图? [英] Sub-pixel accuracy scatter plots with matplotlib?

查看:95
本文介绍了使用 matplotlib 绘制亚像素精度散点图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我在写这个问题的时候想出了一个解决方案.我的答案如下.

有没有一种简单的方法可以使用 matplotlib 获得圆的亚像素抗锯齿放置?我能够创建以下 .gif 文件,但圆圈的运动按整数像素步进这一事实确实让我感到烦恼.

Is there an easy way to get sub-pixel antialiased placement of circles using matplotlib? I was able to create the following .gif but the fact that the motion of the circles steps by integer pixels is really bugging me.

我当然可以渲染大图像 (plt.savefig("image.png",dpi=1000)) 并缩小图像,但增加的复杂性很痛苦(这是一个示例对于学生,因此导入额外的工具会惹恼学生,安装额外的工具会惹恼校园 IT!)

I can of course render a large image (plt.savefig("image.png",dpi=1000)) and scale down, but the added complexity is a pain (this is an example for students, so importing extra tools will annoy the students and installing extra tools will annoy campus IT!)

import matplotlib.pyplot as plt
from math import sin,cos
x=[]
y=[]
#Create 41**2 particles in the unit square
for i in range(41):
    for j in range(41):
        x.append(i*0.05-1.0)
        y.append(j*0.05-1.0)
#5 second video at 30 fps = 150 frames
for n in range(150):
    plt.close('all')
    fig, axes = plt.subplots(figsize=(5,5))
    #some cool motion
    for i in range(len(x)):
        x[i]+=0.001*cos(x[i]+3*y[i])
        y[i]+=0.001*sin(6*x[i]-4*y[i])
    #create the scatter plot
    axes.scatter(x,y,s=3,antialiased=True)
    axes.set_xlim(-1.4,1.4)
    axes.set_ylim(-1.4,1.4)
    axes.set_aspect('equal')
    plt.savefig("out/fig%03d.png"%n,dpi=80)

推荐答案

您可能对 https://感兴趣github.com/anntzer/mplcairo,一个新的基于 cairo 的 Matplotlib 后端,我最初编写它正是,因为我对这个问题不满意.它不会显示上述问题,同时保持合理的性能.

You may be interested in https://github.com/anntzer/mplcairo, a new cairo-based backend for Matplotlib which I originally wrote exactly because I was unhappy with this issue. It does not display the aforementioned problem, while maintaining reasonable performance.

这篇关于使用 matplotlib 绘制亚像素精度散点图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆