matplotlib中的多轴具有不同比例 [英] multiple axis in matplotlib with different scales

查看:156
本文介绍了matplotlib中的多轴具有不同比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Matplotlib中实现多个比例尺?我并不是在说相对于同一x轴绘制的主轴和副轴,而是像许多趋势一样,它们在同一y轴上绘制了不同的比例,并且可以通过它们的颜色来识别.

How can multiple scales can be implemented in Matplotlib? I am not talking about the primary and secondary axis plotted against the same x-axis, but something like many trends which have different scales plotted in same y-axis and that can be identified by their colors.

例如,如果我要根据时间绘制trend1 ([0,1,2,3,4])trend2 ([5000,6000,7000,8000,9000]),并且希望两个趋势具有不同的颜色,并且在Y轴上具有不同的比例,那么如何使用Matplotlib做到这一点?

For example, if I have trend1 ([0,1,2,3,4]) and trend2 ([5000,6000,7000,8000,9000]) to be plotted against time and want the two trends to be of different colors and in Y-axis, different scales, how can I accomplish this with Matplotlib?

当我研究Matplotlib时,他们说尽管现在肯定在他们的心愿单上,但他们暂时还没有这个,是否有办法做到这一点?

When I looked into Matplotlib, they say that they don't have this for now though it is definitely on their wishlist, Is there a way around to make this happen?

还有其他用于python的绘图工具可以实现这一目标吗?

Are there any other plotting tools for python that can make this happen?

推荐答案

如果我理解这个问题,那么您可能会对此示例.

If I understand the question, you may interested in this example in the Matplotlib gallery.

Yann在上面的评论提供了一个类似的例子.

Yann's comment above provides a similar example.

编辑-上面的链接已修复.从Matplotlib图库复制的相应代码:

Edit - Link above fixed. Corresponding code copied from the Matplotlib gallery:

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)

par1 = host.twinx()
par2 = host.twinx()

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right", axes=par2,
                                        offset=(offset, 0))

par2.axis["right"].toggle(all=True)

host.set_xlim(0, 2)
host.set_ylim(0, 2)

host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")

par1.set_ylim(0, 4)
par2.set_ylim(1, 65)

host.legend()

host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())

plt.draw()
plt.show()

#plt.savefig("Test")

这篇关于matplotlib中的多轴具有不同比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆