python图片x轴数据过多_一幅图中的两个(或更多)图形在python中具有不同的x轴和y轴比例...

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)

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.

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

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.

How do I do this?

解决方案

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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值