- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前可以做插图,但只能在 2D 中做。在下面的代码中将 projection='3d'
添加到 fig1.add_axes()
或 inset_axes()
将不起作用。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from mpl_toolkits.axes_grid.inset_locator import inset_axes
x = np.linspace(0,2.0*np.pi,51)
y = np.sin(x)
def make_plot(x,y):
fig = plt.figure(figsize=(10,7))
rect = [0.18,0.18,0.25,0.25]
ax = fig.add_subplot(111)
ax.grid()
ax.set_xlim(x.min(),x.max())
ax.plot(x,y,'bo-')
fig1 = plt.gcf()
ax_inset1 = fig1.add_axes(rect,anchor='NW',axisbg=None) # , projection='3d')
ax_inset2 = inset_axes(ax, width="40%", height=1.8, loc=1) # , projection='3d')
ax_inset1.plot(x,y,'r--'); ax_inset1.set_xlim(x.min(),x.max())
ax_inset2.plot(x,y,'g--'); ax_inset2.set_xlim(x.min(),x.max())
X, Y, Z = axes3d.get_test_data(0.05)
#ax_inset1.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.show()
make_plot(x,y)
上面代码生成的图片如下:
] 1
如何更改上面的代码,使其中一个(或两个)插图具有 3D 图?
最佳答案
晚了几年,但这里是我希望在这里找到的答案。从 matplotlib 3.1.3 开始,这是可行的。
将下面的代码与 fig
参数一起使用应该也允许在 3D 投影上进行“插入”。
import numpy as np
import matplotlib.pyplot as plt
# required for "3d" projection even though not explicitly used
from mpl_toolkits.mplot3d import axes3d
from matplotlib.transforms import Bbox
def add_inset_axes(rect, units="ax", ax_target=None, fig=None, projection=None, **kw):
"""
Wrapper around `fig.add_axes` to achieve `ax.inset_axes` functionality
that works also for insetting 3D plot on 2D ax/figures
"""
assert ax_target is not None or fig is not None, "`fig` or `ax_target` must be provided!"
_units = {"ax", "norm2ax", "norm2fig"}
assert {units} <= _units, "`rect_units` not in {}".format(repr(_units))
if ax_target is not None:
# Inspired from:
# https://stackoverflow.com/questions/14568545/convert-matplotlib-data-units-to-normalized-units
bb_data = Bbox.from_bounds(*rect)
trans = ax_target.transData if units == "ax" else ax_target.transAxes
disp_coords = trans.transform(bb_data)
fig = ax_target.get_figure()
fig_coord = fig.transFigure.inverted().transform(disp_coords)
elif fig is not None:
if ax_target is not None and units != "norm2fig":
bb_data = Bbox.from_bounds(*rect)
trans = ax_target.transData if units == "ax" else ax_target.transAxes
disp_coords = trans.transform(bb_data)
else:
fig_coord = Bbox.from_bounds(*rect)
axin = fig.add_axes(
Bbox(fig_coord),
projection=projection, **kw)
return axin
x = np.linspace(0,2.0*np.pi,51)
y = np.sin(x)
fig = plt.figure(figsize=(10,7))
rect = [0.1, 0.15, 0.4, 0.4]
rect2 = [3.5, 0, 2.5, 1]
ax = fig.add_subplot(111)
ax.grid()
ax.set_xlim(x.min(),x.max())
ax.plot(x,y,'bo-')
# This works actually
ax_inset1 = fig.add_axes(rect, anchor='NW', projection='3d')
ax_inset1.plot(x,y,'r--')
# In case you wanted to make the background of the 3d plot transparent
ax_inset1.patch.set_alpha(0.)
ax_inset1.xaxis.set_alpha(0.)
ax_inset1.yaxis.set_alpha(0.)
ax_inset1.zaxis.set_alpha(0.)
# This one does not work because the inset is assumed to be the same type of plot...
# ax_inset2 = ax.inset_axes(rect, projection='3d')
# Here is a convenient helper function to achieve the same functionaly
# plus some extra sugar
# Convenient wrapper around `fig.add_axes`
ax_inset2 = add_inset_axes(rect2, units="ax", ax_target=ax, projection="3d")
ax_inset2.plot(x,y,'g--')
save_fig(fig, "Stackoverflow.png")
plt.show()
结果:
关于python - 在 Matplotlib 中,如何将 3D 图作为插图包含在内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36331478/
我无法在此图中定位轴标签。我喜欢放置顶部标签,使管道与网格对齐,并放置左右标签,以便它们不接触绘图。 我试过了 ax.tick_params(axis='both', which='both'
我使用的是 python 2,下面的代码只是使用了一些示例数据,我的实际数据可能有不同的长度,并且可能不是很细。 import numpy as np import datetime i
给定坐标 [1,5,7,3,5,10,3,6,8]为 matplotlib.pyplot ,如何突出显示或着色线条的不同部分。例如,列表中的坐标 1-3 ( [1,5,7,3] ) 表示属性 a .我
我正在matplotlib中绘制以下图像。 我的问题是,图像看起来像这样,但是,我想使背景变暗,因为当我打印该图像时,灰度部分不会出现在打印物中。有人可以告诉我API进行此更改吗? 我使用简单的API
这是关于matplotlib的一个非常基本的问题,但是我不知道该怎么做: 我想绘制多个图形,并使用绘制窗口中的箭头从一个移到另一个。 目前,我只知道如何创建多个图并将其绘制在不同的窗口中,如下所示:
在 matplotlib 中绘制小块对象时,由于显示分辨率而引入了伪影。使用抗锯齿并不能解决问题。 这个问题有解决方案吗? import matplotlib.pyplot as plt impo
对于直方图,有一个简单的内置选项 histtype='step' .如何制作相同风格的条形图? 最佳答案 [阅读评论后添加答案] 将可选关键字设置为 fill=False对于条形图: import m
我正在尝试在 (6X3) 网格上创建子图。我对图例的位置有疑问。图例对所有子图都是通用的。 lgend 现在与 y 轴标签重叠 我尝试删除 constrained_layout=True 选项。但这在
我有一个带有一些线段( LineCollection )和一些点的图表。这些线和点有一些与它们相关的值,但没有绘制出来。我希望能够添加鼠标悬停工具提示或其他方法来轻松找到点和线的关联值。这对于点或线段
我想创建一个带有对齐不同曲线文本的图例的图。这是一个最小的工作示例: import matplotlib.pyplot as plt import numpy as np x=np.linspace(
可以说我正在用matplotlib绘制一条线并添加一个图例。 在图例中,其显示为------ Label。当绘制较小的图形尺寸以进行打印时,我发现该行的默认水平长度太长。 是否存在将------ La
我正在使用 matplotlib 构建一个 3D 散点图,但无法使生成的图形具有所有 3 个轴的共同原点。我怎样才能做到这一点? 我的代码(到目前为止),我还没有为轴规范实现任何定义,因为我对 Pyt
我有一个我想使用的绘图布局,其中 9 个不同的数据簇被布置在一个方形网格上。网格中的每个框都包含 3 个并排布置的箱线图。 我最初的想法是这将适合 3x3 子图布局,每个单独的子图本身被划分为 3x1
我的图形从y=-1变为y=10 我想在任意位置写一小段文字,例如x=2000,y=5: ax.annotate('MgII', xy=(2000.0, 5.0), xycoords='data')
我想使用LateX格式来构建一个表达式,其中出现一些数字,但这些数字是用LateX表达式中的变量表示的。 实际的目标是在axes.annotate()方法中使用它,但是为了讨论起见,这里是一个原理代码
我需要比较两组的二维分布。 当我使用 matplotlib.pyplot.contourf并覆盖图,每个等高线图的背景颜色填充整个图空间。有没有办法让每个等高线图的最低等高线级别透明,以便更容易看到每
在R中,有一个locator函数,类似于Matlab的ginput,您可以用鼠标单击图形并选择任何x,y坐标。此外,还有一个名为identify(x,y)的函数,如果您给它绘制了一组绘制的点x,y,然
我想用matplotlib生成矢量图。我尽力了-但输出是光栅图像。这是我使用的: import matplotlib matplotlib.use('Agg') import matplotlib.p
我正在尝试使用 matplotlib 制作具有非常小的灰点的散点图。由于点密度的原因,点需要很小。问题是 scatter() 函数的标记似乎既有线条又有填充。当标记很小时,只有线条可见,而看不到填充,
我不太明白为什么我无法在指定的限制内创建水平和垂直线。我想用这个框绑定(bind)数据。然而,双方似乎并没有遵守我的指示。为什么是这样? # CREATING A BOUNDING BOX # BOT
我是一名优秀的程序员,十分优秀!