如何在Jupyter Notebook中放大内联图?

问题:如何在Jupyter Notebook中放大内联图?

我已在我的Ipython Notebook上使用“ %matplotlib inline” 内嵌我的图。

现在,该图出现。但是,它很小。有没有办法使用笔记本设置或绘图设置使其显得更大?

I have made my plots inline on my Ipython Notebook with “%matplotlib inline.”

Now, the plot appears. However, it is very small. Is there a way to make it appear larger using either notebook settings or plot settings?


回答 0

是的,figuresize像这样玩(在调用子图之前):

fig=plt.figure(figsize=(18, 16), dpi= 80, facecolor='w', edgecolor='k')

Yes, play with figuresize and dpi like so (before you call your subplot):

fig=plt.figure(figsize=(12,8), dpi= 100, facecolor='w', edgecolor='k')

As @tacaswell and @Hagne pointed out, you can also change the defaults if it’s not a one-off:

plt.rcParams['figure.figsize'] = [12, 8]
plt.rcParams['figure.dpi'] = 100 # 200 e.g. is really fine, but slower

回答 1

默认图形大小(以英寸为单位)由

matplotlib.rcParams['figure.figsize'] = [width, height]

例如:

import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [10, 5]

创建一个10(宽)x 5(高)英寸的图形

The default figure size (in inches) is controlled by

matplotlib.rcParams['figure.figsize'] = [width, height]

For example:

import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [10, 5]

creates a figure with 10 (width) x 5 (height) inches


回答 2

我发现 %matplotlib notebook与内联Jupyter笔记本相比,这种方法对我来说更好。

请注意,如果您以前使用%matplotlib inline过,则可能需要重新启动内核。

2019年更新:如果您正在运行Jupyter Lab,则可能要使用 %matplotlib widget

I have found that %matplotlib notebook works better for me than inline with Jupyter notebooks.

Note that you may need to restart the kernel if you were using %matplotlib inline before.

Update 2019: If you are running Jupyter Lab you might want to use %matplotlib widget


回答 3

如果您只希望图形显示更大而不改变图形的一般外观,则可以提高图形分辨率。根据大多数其他答案中的建议更改图形大小会改变外观,因为字体大小不会相应缩放。

import matplotlib.pylab as plt
plt.rcParams['figure.dpi'] = 200

If you only want the image of your figure to appear larger without changing the general appearance of your figure increase the figure resolution. Changing the figure size as suggested in most other answers will change the appearance since font sizes do not scale accordingly.

import matplotlib.pylab as plt
plt.rcParams['figure.dpi'] = 200

回答 4

问题是关于matplotlib,但是为了所有最终在此处获得与语言无关的标题的R用户的缘故:

如果您使用的是R内核,则只需使用:

options(repr.plot.width=4, repr.plot.height=3)

The question is about matplotlib, but for the sake of any R users that end up here given the language-agnostic title:

If you’re using an R kernel, just use:

options(repr.plot.width=4, repr.plot.height=3)

回答 5

调整一个图形的大小:

import matplotlib.pyplot as plt

fig=plt.figure(figsize=(15, 15))

要更改默认设置,从而更改所有图,请执行以下操作:

import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = [15, 15]

To adjust the size of one figure:

import matplotlib.pyplot as plt

fig=plt.figure(figsize=(15, 15))

To change the default settings, and therefore all your plots:

import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = [15, 15]


回答 6

一次性调整图形大小的一个小而重要的细节(如上述几位评论者所说,“这对我不起作用”):

您应该在定义实际图之前做plt.figure(figsize =(,))。例如:

这应该根据您指定的figsize正确调整图的大小:

values = [1,1,1,2,2,3]
_ = plt.figure(figsize=(10,6))
_ = plt.hist(values,bins=3)
plt.show()

而这将显示具有默认设置的图,似乎“忽略”了figsize:

values = [1,1,1,2,2,3]
_ = plt.hist(values,bins=3)
_ = plt.figure(figsize=(10,6))
plt.show()

A small but important detail for adjusting figure size on a one-off basis (as several commenters above reported “this doesn’t work for me”):

You should do plt.figure(figsize=(,)) PRIOR to defining your actual plot. For example:

This should correctly size the plot according to your specified figsize:

values = [1,1,1,2,2,3]
_ = plt.figure(figsize=(10,6))
_ = plt.hist(values,bins=3)
plt.show()

Whereas this will show the plot with the default settings, seeming to “ignore” figsize:

values = [1,1,1,2,2,3]
_ = plt.hist(values,bins=3)
_ = plt.figure(figsize=(10,6))
plt.show()