在python中具有不同x轴和y轴刻度的两个(或更多)图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42734109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
two (or more) graphs in one plot with different x-axis AND y-axis scales in python
提问by Anton Rodenhauser
I want 3 graphs on one axes object, for example:
我想要一个轴对象上的 3 个图形,例如:
#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)
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.
现在我想将所有三个数据集添加到 ax。但是他们不应该共享任何 x 轴或 y 轴(从那时起,由于不同的比例,一个会比另一个小。我需要像 ax.twinx()、ax.twiny() 这样的东西,但是 x - 和 y 轴需要独立。
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"). Plot1Plot2
我想这样做,因为我想把两个附加的图(和第三个,类似于第二个)放在一个图中(“把它们放在一起”)。 地块 1地块 2
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.
然后,我会将第二个图的 x/y 标签(和/或刻度、限制)放在右侧/顶部,并将另一个图的 x/y 限制放在底部/左侧。我不需要 3. 情节的 x/y 标签。
How do I do this?
我该怎么做呢?
回答by ImportanceOfBeingErnest
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")
.
这个想法是在同一位置创建三个子图。为了确保它们将被识别为不同的图,它们的属性需要不同 - 实现这一点的最简单方法就是提供不同的标签,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()
回答by Tristan
You could also standardize the data so it shares the same limits and then plot the limits of the desired second scale "manually". This function standardizes the data to the limits of the first set of points:
您还可以标准化数据,使其共享相同的限制,然后“手动”绘制所需的第二个比例的限制。此函数将数据标准化为第一组点的限制:
def standardize(data):
for a in range(2):
span = max(data[0][a]) - min(data[0][a])
min_ = min(data[0][a])
for idx in range(len(data)):
standardize = (max(data[idx][a]) - min(data[idx][a]))/span
data[idx][a] = [i/standardize + min_ - min([i/standardize
for i in data[idx][a]]) for i in data[idx][a]]
return data
Then, plotting the data is easy:
然后,绘制数据很容易:
import matplotlib.pyplot as plt
data = [[[1,2,3,4,5],[1,2,2,4,1]], [[-1000,-800,-600,-400,-200], [10,20,39,40,50]], [[150,200,250,300,350], [10,20,30,40,50]]]
limits = [(min(data[1][a]), max(data[1][a])) for a in range(2)]
norm_data = standardize(data)
fig, ax = plt.subplots()
for x, y in norm_data:
ax.plot(x, y)
ax2, ax3 = ax.twinx(), ax.twiny()
ax2.set_ylim(limits[1])
ax3.set_xlim(limits[0])
plt.show()
Since all data points have the limits of the first set of points, we can just plot them on the same axis. Then, using the limits of the desired second x and y axis we can set the limits for these two.
由于所有数据点都有第一组点的限制,我们可以将它们绘制在同一轴上。然后,使用所需的第二个 x 轴和 y 轴的限制,我们可以为这两个轴设置限制。