不同比例的叠加图
到目前为止,我有以下代码:
colors = ('k','r','b')
ax = []
for i in range(3):
ax.append(plt.axes())
plt.plot(datamatrix[:,0],datamatrix[:,i],colors[i]+'o')
ax[i].set(autoscale_on=True)
使用每个轴的 autoscale_on=True
选项,我认为每个图都应该有自己的 y 轴限制,但看起来它们都共享相同的值(即使它们共享不同的轴)。如何将它们设置为缩放以显示每个 datamatrix[:,i] 的范围(只需显式调用 .set_ylim()
?)而且,如何我为上面可能需要的第三个变量 (datamatrix[:,2]
) 创建一个偏移 y 轴?谢谢大家。
So far I have the following code:
colors = ('k','r','b')
ax = []
for i in range(3):
ax.append(plt.axes())
plt.plot(datamatrix[:,0],datamatrix[:,i],colors[i]+'o')
ax[i].set(autoscale_on=True)
With the autoscale_on=True
option for each axis, I thought each plot should have its own y-axis limits, but it appears they all share the same value (even if they share different axes). How do I set them to scale to show the range of each datamatrix[:,i]
(just an explicit call to .set_ylim()
?) And also, how can I create an offset y-axis for the third variable (datamatrix[:,2]
) that might be required above? Thanks all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来你想要的是子图......你现在所做的没有多大意义(或者我对你的代码片段感到非常困惑,无论如何......)。
尝试更多类似这样的内容:
编辑:
如果您不需要子图,您的代码片段更有意义。
您正在尝试将三个轴添加到彼此的顶部。 Matplotlib 认识到图形上已经存在一个大小和位置完全相同的子图,因此每次都会返回相同 轴对象。换句话说,如果您查看列表
ax
,您会发现它们都是同一个对象。如果您确实想要这样做,则每次添加轴时都需要将
fig._seen
重置为空字典。然而,您可能并不真的想这样做。不要将三个独立的图放在一起,而是使用
twinx
来代替。例如
It sounds like what you're wanting is subplots... What you're doing now doesn't make much sense (Or I'm very confused by your code snippet, at any rate...).
Try something more like this:
Edit:
If you don't want subplots, your code snippet makes a lot more sense.
You're trying to add three axes right on top of each other. Matplotlib is recognizing that there's already a subplot in that exactly size and location on the figure, and so it's returning the same axes object each time. In other words, if you look at your list
ax
, you'll see that they're all the same object.If you really want to do that, you'll need to reset
fig._seen
to an empty dict each time you add an axes. You probably don't really want to do that, however.Instead of putting three independent plots over each other, have a look at using
twinx
instead.E.g.
使用@joe-kington's引导快速绘制共享x轴的多个y轴图表回答:
Bootstrapping something fast to chart multiple y-axes sharing an x-axis using @joe-kington's answer:
感谢 Joe Kington 的回答,我可以想出一个解决方案来满足我的要求,即所有附加 y 轴都位于图表的左侧。
我仍然想知道如何正确执行此操作,因为这只是一种解决方法:
Thanks to Joe Kington's answer I could come up with a solution for my requirement that all additional y-axis are on the left hand side of the graph.
I still would like to know how to do it correct, because it's just a work around:
双胞胎
。简短示例:twinx
. Short example:我使用了这段代码,它成功生成了两个 Y 轴(主要和次要),并具有从 Excel 文件读取数据所需的比例:
I've used this code and it successfully generated two Y-axes (primary & secondary) with desired scales reading data from Excel file: