一个绘图中的两个(或多个)图形在python中具有不同的x轴和y轴比例 [英] two (or more) graphs in one plot with different x-axis AND y-axis scales in python

查看:1332
本文介绍了一个绘图中的两个(或多个)图形在python中具有不同的x轴和y轴比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在一个坐标轴对象上显示3个图形,例如:

I want 3 graphs on one axes object, for example:

#example x- and y-data
x_values1=[1,2,3,4,5]
y_values1=[1,2,3,4,5]

x_values2=[-1000,-800,-600,-400,-200]
y_values2=[10,20,39,40,50]

x_values3=[150,200,250,300,350]
y_values3=[10,20,30,40,50]


#make axes
fig=plt.figure()
ax=fig.add_subplot(111)

现在我想将所有三个数据集添加到ax.但是,它们不应共享任何x轴或y轴(因为那样,由于比例不同,一个将比另一个小得多.我需要ax.twinx(),ax.twiny之类的东西. (),但x和y轴都必须是独立的.

now I want to add all three data sets to ax. But they shouldn't share any x- or y-axis (since then because of the diffenrent scales one would be way smaller thant the other. I need something like ax.twinx(), ax.twiny(), but both the x- and y-axis need to be independent.

我想这样做,因为我想将两个附加的图(第三个图与第二个图相似)放在一个图中(将它们放在彼此之上"). 情节1 情节2

I want to do this, because I want to put the two attached plots (and a third one, that is similar to the second one) in one plot ("put them on top of each other"). Plot1 Plot2

然后我将第二个绘图的x/y标签(和/或刻度,界限)放在右/顶部,另一个绘图的x/y界限在底部/左侧.我不需要3.情节的x/y标签.

I then would put the x/y-labels (and/or ticks, limits) of the second plot on the right/top and the x/y-limits of another plot in the bottom/left. I dont need x/y-labels of the 3. plot.

我该怎么做?

推荐答案

该想法是在同一位置创建三个子图.为了确保将它们识别为不同的图,它们的属性需要不同-实现此目的的最简单方法是简单地提供一个不同的标签ax=fig.add_subplot(111, label="1").

The idea would be to create three subplots at the same position. In order to make sure, they will be recognized as different plots, their properties need to differ - and the easiest way to achieve this is simply to provide a different label, ax=fig.add_subplot(111, label="1").

其余的只是简单地调整所有轴参数,以使生成的图看起来很吸引人. 设置所有参数需要一些工作,但是以下操作应该可以满足您的需求.

The rest is simply adjusting all the axes parameters, such that the resulting plot looks appealing. It's a little bit of work to set all the parameters, but the following should do what you need.

import matplotlib.pyplot as plt

x_values1=[1,2,3,4,5]
y_values1=[1,2,2,4,1]

x_values2=[-1000,-800,-600,-400,-200]
y_values2=[10,20,39,40,50]

x_values3=[150,200,250,300,350]
y_values3=[10,20,30,40,50]


fig=plt.figure()
ax=fig.add_subplot(111, label="1")
ax2=fig.add_subplot(111, label="2", frame_on=False)
ax3=fig.add_subplot(111, label="3", frame_on=False)

ax.plot(x_values1, y_values1, color="C0")
ax.set_xlabel("x label 1", color="C0")
ax.set_ylabel("y label 1", color="C0")
ax.tick_params(axis='x', colors="C0")
ax.tick_params(axis='y', colors="C0")

ax2.scatter(x_values2, y_values2, color="C1")
ax2.xaxis.tick_top()
ax2.yaxis.tick_right()
ax2.set_xlabel('x label 2', color="C1") 
ax2.set_ylabel('y label 2', color="C1")       
ax2.xaxis.set_label_position('top') 
ax2.yaxis.set_label_position('right') 
ax2.tick_params(axis='x', colors="C1")
ax2.tick_params(axis='y', colors="C1")

ax3.plot(x_values3, y_values3, color="C3")
ax3.set_xticks([])
ax3.set_yticks([])

plt.show()

这篇关于一个绘图中的两个(或多个)图形在python中具有不同的x轴和y轴比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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