我想创建一个功能,在一个窗口中在屏幕上绘制一组图形.到现在为止我写这段代码:
import pylab as pl
def plot_figures(figures):
"""Plot a dictionary of figures.
Parameters
----------
figures : <title, figure> dictionary
"""
for title in figures:
pl.figure()
pl.imshow(figures[title])
pl.gray()
pl.title(title)
pl.axis('off')
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但我想有选择在单个窗口中绘制所有数字.而这段代码没有.我读了一些关于subplot的东西,但看起来很棘手.
我试图使用子图绘制许多数据,我没有遇到麻烦,但我想知道是否有一种方便的方法来做到这一点.
下面是示例代码.
import numpy as np
import math
import matplotlib.pyplot as plt
quantities=["sam_mvir","mvir","rvir","rs","vrms","vmax"
,"jx","jy","jz","spin","m200b","m200c","m500c","m2500c"
,"xoff","voff","btoc","ctoa","ax","ay","az"]
# len(quantities) = 21, just to make the second loop expression
# shorter in this post.
ncol = 5
nrow = math.ceil(21 / ncol)
fig, axes = plt.subplots(nrows = nrow, ncols=ncol, figsize=(8,6))
for i in range(nrow):
for j in range(((21-i*5)>5)*5 + ((21-i*5)<5)*(21%5)):
axes[i, j].plot(tree[quantities[i*ncol + j]])
axes[i, j].set_title(quantities[i*ncol + j])
Run Code Online (Sandbox Code Playgroud)
此代码循环遍历2D子阵列,并在第21个图中停止,留下4个面板.我的问题是,有没有内置的方法来完成这项任务?例如,制作2D子图阵列并将阵列"展平"为1D,然后在1D阵列上循环0到20.
第二个范围()中的表达式非常难看.我不认为我会使用这段代码.我认为琐碎的方法是计算图表的数量,如果计数> 21则中断.但我只是想知道是否有更好的(或奇特的)方式.
我创建了两个函数来绘制两个特定的图并返回各自的数字:
import matplotlib.pyplot as plt
x = range(1,100)
y = range(1,100)
def my_plot_1(x,y):
fig = plt.plot(x,y)
return fig
def my_plot_2(x,y):
fig = plt.plot(x,y)
return fig
Run Code Online (Sandbox Code Playgroud)
现在,在我的函数之外,我想创建一个带有两个子图的图形并将我的函数图形添加到其中。像这样的东西:
my_fig_1 = my_plot_1(x,y)
my_fig_2 = my_plot_2(x,y)
fig, fig_axes = plt.subplots(ncols=2, nrows=1)
fig_axes[0,0] = my_fig_1
fig_axes[0,1] = my_fig_2
Run Code Online (Sandbox Code Playgroud)
然而,仅仅将创建的图形分配给这个新图形是行不通的。该函数调用该图窗,但未在子图中分配该图窗。有没有办法将我的函数图放置在另一个图的子图中?
我正在使用 matplotlib(版本 1.3.1)和 Python 2.7 绘制子图(6x2)网格。我设置了我的图形并在子图中绘制如下所示的内容:
fig, axes = plt.subplots(ncols=6, nrows=2,
sharex=True, sharey=True, figsize=(30,5))
axes[0,0].plot(x, y)
axes[1,5].plot(z, a)
Run Code Online (Sandbox Code Playgroud)
等等。
我的问题是:有没有办法一次更改所有这些图上的线属性?我可以axes[0,0].plot(x,y,'k',linewidth=2.0)在每个轴上手动指定,但我认为必须有一种方法可以同时为所有 12 个图执行此操作。
干杯。
我正在 python (v 2.7.9) 中使用 matplotlib (v 1.4.2) 绘制子图网格。我可以手动调整子图之间的间距,但我希望仅某些子图具有不同的间距。我希望的最终图形是左侧有一个 2x5 子图网格,右侧有一个 2x5 子图网格,中间有一个空格。
我用来控制图形布局的代码如下:
figw, figh = 16.5, 15.0 #18.5, 15.0
fig, axes = plt.subplots(ncols=4, nrows=5, sharex=False,
sharey=True, figsize=(figw, figh))
plt.subplots_adjust(hspace=0.0, wspace=0.2, left=1/figw,
right=1-2./figw, bottom=1/figh, top=1-2./figh)
Run Code Online (Sandbox Code Playgroud)
当我改变时wspace,我得到 4 列均等间距。有没有办法wspace以这样的方式进行更改:第 0 列和第 1 列之间为 0,第 1 列和第 2 列之间为 x,第 2 列和第 3 列之间为 0?
谢谢。
我有一个类似的问题,以前回答过。但是,它与 Pandas 包的用法不同。
这是我之前的问题:matplotlib - 子图中的 twinx 轴没有 xlabel 和 xticks
所以,我和上一个一样的问题是,为什么在使用此 Python 代码时,第一行图不显示 xlabel 和 xticks。
两个注意事项:
subplots而不是gridspec相同的结果。import matplotlib.pyplot as plt
import matplotlib.gridspec as gspec
import numpy as np
import pandas as pd
from math import sqrt
fig = plt.figure()
gs = gspec.GridSpec(2, 2)
gs.update(hspace=0.7, wspace=0.7)
ax1 = plt.subplot(gs[0, 0])
ax2 = plt.subplot(gs[0, 1])
ax3 = plt.subplot(gs[1, 0])
ax4 = plt.subplot(gs[1, 1])
x1 = …Run Code Online (Sandbox Code Playgroud) 一旦我在图中创建了一个子图系统
fig, ((ax1, ax2)) = plt.subplots(1, 2)
Run Code Online (Sandbox Code Playgroud)
我可以玩转 的位置ax2吗,例如,将它向右或向左移动一点?
换句话说,在将坐标区对象创建为子图元素后,我可以自定义它在图形中的位置吗?如果是这样,我该如何编码?
谢谢你的思考
我有一个由 3 个子图组成的图形。我想将最后一个子图定位在第二行中间。目前它位于图的左下角。我该怎么做呢?我找不到堆栈溢出的答案。
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(13,10))
ax= axes.flatten()
ax[0].plot(vDT, np.cumsum(mWopt0[asset0,:])*percentage/iTT, label= 'COAL, c = 0')
ax[0].legend()
ax[0].set_title('Proportion in most invested stock')
ax[1].plot(vDT, np.cumsum(mWopt01[asset01,:])*percentage/iTT, label= 'OINL, c = 0.1')
ax[1].plot(vDT, np.cumsum(mWopt03[asset03,:])*percentage/iTT, label= 'OINL, c = 0.3')
ax[1].plot(vDT, np.cumsum(mWopt05[asset05,:])*percentage/iTT, label= 'OINL, c = 0.5')
ax[1].plot(vDT, np.cumsum(mWopt2[asset2,:])*percentage/iTT, label= 'OINL, c = 2')
ax[1].plot(vDT, np.cumsum(mWopt5[asset5,:])*percentage/iTT, label= 'OINL, c = 5')
ax[1].plot(vDT, np.cumsum(mWopt10[asset10,:])*percentage/iTT, label= 'OINL, c = 10')
ax[1].legend()
ax[1].set_title('Proportion in most invested stock')
ax[2].plot(vDT, np.cumsum(mWopt01[index,:])*percentage/iTT, label= 'c = 0')
ax[2].plot(vDT, np.cumsum(mWopt01[index,:])*percentage/iTT, …Run Code Online (Sandbox Code Playgroud) 我有几个子图,想要通过 ax.tick_params 调整轴刻度设置。一切工作正常,但是,没有显示小刻度。这是一个代码示例
import matplotlib.pyplot as plt
x = np.linspace(0,1,100)
y = x*x
f, (ax1,ax2) = plt.subplots(2, 1)
ax1.tick_params(axis="both", direction="in", which="both", right=False, top=True)
ax2.tick_params(axis="both", direction="in", which="both", right=True, top=False)
ax1.plot(x,y)
ax2.plot(x,-y)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我认为 which=both 会给我小刻度。但是我需要添加一个额外的
plt.minorticks_on()
Run Code Online (Sandbox Code Playgroud)
这使得它们可见,但仅在 ax2 中可见。
我该如何解决?
我正在使用Octave在同一个图中绘制子图,并且想要添加一般标题,是否有像Matlabsgtitle中那样的内置函数,因为 这个老问题
有一个解决方案,但没有使用任何很酷的内置函数,而 Octave 没有。也在这里Octave - Bugs:bug #55019,我发现Octave 中有新的 sgtitle 函数,但找不到文档或包名称或任何东西......它是否尚未完全实现或有其他替代方案???sgtitle
我问的是 package 或 bultin 函数而不是实现,我将从某人那里编写或复制