Python 如何设置散点图的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39712767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:36:53  来源:igfitidea点击:

How to set size for scatter plot

pythonmatplotlib

提问by AntonK

I have three lines setting a plot. I expect the size of the pic.png to be 640x640 pixels. But I got 800x800 picture.

我有三行设置情节。我希望 pic.png 的大小为 640x640 像素。但是我得到了 800x800 的图片。

plt.figure(figsize=(8, 8), dpi=80)
plt.scatter(X[:],Y[:])
plt.savefig('pic.png')

BTW I have no problem setting size with object-oriented interface but I need to use pyplot style.

顺便说一句,使用面向对象的界面设置大小没有问题,但我需要使用 pyplot 样式。

回答by Alberto Garcia-Raboso

The following code produces a 576x576 PNG image in my machine:

以下代码在我的机器中生成一个 576x576 的 PNG 图像:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y = np.random.rand(10)

plt.figure(figsize=(8, 8), dpi=80)
plt.scatter(x, y)
plt.savefig('pic.png')

Shifting dpi=80to the plt.savefigcall correctly results in a 640x640 PNG image:

转移dpi=80plt.savefig呼叫正确导致640x640 PNG图像:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y = np.random.rand(10)

plt.figure(figsize=(8, 8))
plt.scatter(x, y)
plt.savefig('pic.png', dpi=80)

I can't offer any explanation as to why this happens though.

我无法解释为什么会发生这种情况。

回答by wflynny

While Alberto's answer gives you the correct work around, looking at the documentation for plt.savefiggives you a better idea as to why this behavior happens.

虽然 Alberto 的回答为您提供了正确的解决方法,但查看文档可以plt.savefig让您更好地了解为什么会发生这种行为。

dpi: [ None | scalar > 0 | ‘figure'] The resolution in dots per inch. If None it will default to the value savefig.dpi in the matplotlibrc file. If ‘figure' it will set the dpi to be the value of the figure.

dpi: [ 无 | 标量 > 0 | '数字'] 每英寸点数的分辨率。如果没有,它将默认为 matplotlibrc 文件中的 savefig.dpi 值。如果是 'figure',它会将 dpi 设置为数字的值。

Short answer:use plt.savefig(..., dpi='figure')to use the dpi value set at figure creation.

简短回答:用于plt.savefig(..., dpi='figure')使用图形创建时设置的 dpi 值。

Longer answer:with a plt.figure(figsize=(8,8), dpi=80), you can get a 640x640 figure in the following ways:

更长的答案:使用 a plt.figure(figsize=(8,8), dpi=80),您可以通过以下方式获得 640x640 图形:

  1. Pass the dpi='figure'keyword argument to plt.savefig:

    plt.figure(figsize=(8, 8), dpi=80)
    ...
    plt.savefig('pic.png', dpi='figure')
    
  2. Explicitly give the dpi you want to savefig:

    plt.figure(figsize=(8, 8))
    ...
    plt.savefig('pic.png', dpi=80)
    
  3. Edit your matplotlibrc fileat the following line:

    #figure.dpi       : 80      # figure dots per inch
    

    then

    plt.figure(figsize=(8, 8))
    ...
    plt.savefig('pic.png') #dpi=None implicitly defaults to rc file value
    
  1. dpi='figure'关键字参数传递给plt.savefig

    plt.figure(figsize=(8, 8), dpi=80)
    ...
    plt.savefig('pic.png', dpi='figure')
    
  2. 明确给出你想要的 dpi savefig

    plt.figure(figsize=(8, 8))
    ...
    plt.savefig('pic.png', dpi=80)
    
  3. 在以下行编辑您的matplotlibrc 文件

    #figure.dpi       : 80      # figure dots per inch
    

    然后

    plt.figure(figsize=(8, 8))
    ...
    plt.savefig('pic.png') #dpi=None implicitly defaults to rc file value