mcs*_*her 260 python matplotlib
与这个问题非常相似,但不同之处在于我的身材可以达到需要的大小.
我需要在matplotlib中生成一堆垂直堆叠的图.结果将使用figsave保存并在网页上查看,因此我不关心最终图像的高度,只要子图间隔开,这样它们就不会重叠.
无论我有多大的数字,子图总是似乎重叠.
我的代码目前看起来像
import matplotlib.pyplot as plt
import my_other_module
titles, x_lists, y_lists = my_other_module.get_data()
fig = plt.figure(figsize=(10,60))
for i, y_list in enumerate(y_lists):
plt.subplot(len(titles), 1, i)
plt.xlabel("Some X label")
plt.ylabel("Some Y label")
plt.title(titles[i])
plt.plot(x_lists[i],y_list)
fig.savefig('out.png', dpi=100)
Run Code Online (Sandbox Code Playgroud)
Joe*_*ton 357
尝试使用 plt.tight_layout
作为一个简单的例子:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=4, ncols=4)
fig.tight_layout() # Or equivalently, "plt.tight_layout()"
plt.show()
Run Code Online (Sandbox Code Playgroud)
没有严格的布局
紧密的布局
Cya*_*ook 255
您可以使用plt.subplots_adjust
更改子图之间的间距(源)
通话签名:
subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
Run Code Online (Sandbox Code Playgroud)
参数含义(和建议的默认值)是:
left = 0.125 # the left side of the subplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = 0.2 # the amount of width reserved for blank space between subplots
hspace = 0.2 # the amount of height reserved for white space between subplots
Run Code Online (Sandbox Code Playgroud)
实际默认值由rc文件控制
Ale*_*ord 50
我发现subplots_adjust(hspace = 0.001)最终为我工作了.当我使用space = None时,每个绘图之间仍然有空白区域.将它设置为非常接近零但似乎迫使它们排成一行.我在这里上传的不是最优雅的代码,但你可以看到hspace的工作原理.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tic
fig = plt.figure()
x = np.arange(100)
y = 3.*np.sin(x*2.*np.pi/100.)
for i in range(5):
temp = 510 + i
ax = plt.subplot(temp)
plt.plot(x,y)
plt.subplots_adjust(hspace = .001)
temp = tic.MaxNLocator(3)
ax.yaxis.set_major_locator(temp)
ax.set_xticklabels(())
ax.title.set_visible(False)
plt.show()
Run Code Online (Sandbox Code Playgroud)
The*_*emz 30
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,60))
plt.subplots_adjust( ... )
Run Code Online (Sandbox Code Playgroud)
该plt.subplots_adjust方法:
def subplots_adjust(*args, **kwargs):
"""
call signature::
subplots_adjust(left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None)
Tune the subplot layout via the
:class:`matplotlib.figure.SubplotParams` mechanism. The parameter
meanings (and suggested defaults) are::
left = 0.125 # the left side of the subplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = 0.2 # the amount of width reserved for blank space between subplots
hspace = 0.2 # the amount of height reserved for white space between subplots
The actual defaults are controlled by the rc file
"""
fig = gcf()
fig.subplots_adjust(*args, **kwargs)
draw_if_interactive()
Run Code Online (Sandbox Code Playgroud)
要么
fig = plt.figure(figsize=(10,60))
fig.subplots_adjust( ... )
Run Code Online (Sandbox Code Playgroud)
图片的大小很重要.
"我已经尝试过使用hspace,但增加它似乎只会使所有图形更小而不解决重叠问题."
因此,为了产生更多的空白空间并保持子图的大小,总图像需要更大.
Cia*_*lsh 23
你可以试试subplot_tool()
plt.subplot_tool()
Run Code Online (Sandbox Code Playgroud)
Imp*_*est 11
tight_layout
现在提供了类似于matplotlib(从2.2版开始)的功能constrained_layout
。与相比tight_layout
,可以在代码中随时针对单个优化布局调用,这constrained_layout
是一个属性,该属性可以处于活动状态,并将在每个绘制步骤之前优化布局。
因此,需要在创建子图之前或期间激活它,例如figure(constrained_layout=True)
或subplots(constrained_layout=True)
。
例:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(4,4, constrained_layout=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
constrained_layout也可以通过设置 rcParams
plt.rcParams['figure.constrained_layout.use'] = True
Run Code Online (Sandbox Code Playgroud)
pandas.DataFrame.plot
,该数据框用作matplotlib
默认后端。
kind=
指定的内容(例如'bar'
、'scatter'
、'hist'
等)。python 3.8.12
, pandas 1.3.4
,matplotlib 3.4.3
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# sinusoidal sample data
sample_length = range(1, 15+1)
rads = np.arange(0, 2*np.pi, 0.01)
data = np.array([np.sin(t*rads) for t in sample_length])
df = pd.DataFrame(data.T, index=pd.Series(rads.tolist(), name='radians'), columns=[f'freq: {i}x' for i in sample_length])
# default plot with subplots; each column is a subplot
axes = df.plot(subplots=True)
Run Code Online (Sandbox Code Playgroud)
pandas.DataFrame.plot
figsize
:每个子图的宽度为 5,高度为 4 是一个很好的起点。layout
:(行、列)子图的布局。sharey=True
因此sharex=True
,每个子图上的冗余标签都不会占用空间。.plot
方法返回一个 numpy 数组matplotlib.axes.Axes
,该数组应该被展平以便于使用。.get_figure()
提取图形对象。DataFrame.plot
Axes
fig.tight_layout()
。axes = df.plot(subplots=True, layout=(3, 5), figsize=(25, 16), sharex=True, sharey=True)
# flatten the axes array to easily access any subplot
axes = axes.flat
# extract the figure object
fig = axes[0].get_figure()
# use tight_layout
fig.tight_layout()
Run Code Online (Sandbox Code Playgroud)
df
# display(df.head(3))
freq: 1x freq: 2x freq: 3x freq: 4x freq: 5x freq: 6x freq: 7x freq: 8x freq: 9x freq: 10x freq: 11x freq: 12x freq: 13x freq: 14x freq: 15x
radians
0.00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.01 0.010000 0.019999 0.029996 0.039989 0.049979 0.059964 0.069943 0.079915 0.089879 0.099833 0.109778 0.119712 0.129634 0.139543 0.149438
0.02 0.019999 0.039989 0.059964 0.079915 0.099833 0.119712 0.139543 0.159318 0.179030 0.198669 0.218230 0.237703 0.257081 0.276356 0.295520
Run Code Online (Sandbox Code Playgroud)
fig.tight_layout
显示了创建图形后的使用。但是,tight_layout
可以在创建图形时设置,因为matplotlib.pyplot.subplots
接受带有 的附加参数**fig_kw
。所有附加关键字参数都会传递给pyplot.figure
调用。import matplotlib.pyplot as plt
# create the figure with tight_layout=True
fig, axes = plt.subplots(nrows=4, ncols=4, figsize=(8, 8), tight_layout=True)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
318032 次 |
最近记录: |