在具有特殊x2和y2轴的matplotlib中制作散点图 [英] Making a scatter plot in matplotlib with special x2 and y2 axes

查看:133
本文介绍了在具有特殊x2和y2轴的matplotlib中制作散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是绘图的新手.我已经在python中使用matplotlib编写了以下代码以构建散点图:

I am a newbie in drawing plots. I have written the following code with matplotlib in python to build a scatterplot:

import numpy as np
import matplotlib.pyplot as plt
Edge_matrix=[[269, 270], [270, 269], [274, 275], [275, 274], [341, 342], 
[342, 341], [711, 712], [712, 711]]
x=[]; y=[]
for i in Edge_matrix:
    x.append(i[0])
    y.append(i[1])
#print(x,y)

plt.scatter(x,y, s=1, c='blue', alpha=1)
plt.axhline(576, linewidth=0.3, color='blue', label='zorder=2', zorder=2)
plt.axvline(576, linewidth=0.3, color='blue', label='zorder=2', zorder=2)
plt.show()

我希望x1和y1轴从0到821开始,并使新的x2轴从1到577到垂直线,并在通过垂直线后再次从1到243起始;我需要一个与x2完全一样的新y2轴.有什么方法可以更改我的代码以获得我最喜欢的情节吗? 这是运行代码后的图:

I want the x1 and y1 axes start from 0 to 821 and make new x2 axis starting from 1 to 577 to the vertical line and after passing vertical line, again starting from 1 to 243; I need a new y2 axis exactly like x2. Is there any way to change my code for getting my favorite plot? This is the plot after running the code:

我想要的情节如下:

The plot I would like to have is the following:

推荐答案

您可以使用双轴生成另一个轴,您可以为其设置不同的刻度和刻度标签.

You may use twin axes to produce another axes for which you may set different ticks and ticklabels.

import numpy as np
import matplotlib.pyplot as plt

Edge_matrix=[[269, 270], [270, 269], [274, 275], [275, 274], [341, 342], 
[342, 341], [711, 712], [712, 711]]
x,y = zip(*Edge_matrix)

limits = [0,821]
sec_lim = 243
bp = 576

ax = plt.gca()
ax.set_xlim(limits)
ax.set_ylim(limits)
ax.scatter(x,y, s=1, c='blue', alpha=1)
ax.axhline(bp, linewidth=0.3, color='blue', label='zorder=2', zorder=2)
ax.axvline(bp, linewidth=0.3, color='blue', label='zorder=2', zorder=2)

ax2 = ax.twinx()
ax2.yaxis.tick_right()
ax2.set_ylim(ax.get_ylim())
yticks = ax.get_yticks()
ax2.set_yticks(np.append(yticks[yticks<bp], [bp,bp+sec_lim]) )
ax2.set_yticklabels(np.append(yticks[yticks<bp], [0,sec_lim]).astype(int) )

ax3 = ax.twiny()
ax3.xaxis.tick_top()
ax3.set_xlim(ax.get_xlim())
xticks = ax.get_xticks()
ax3.set_xticks(np.append(xticks[xticks<bp], [bp,bp+sec_lim]) )
ax3.set_xticklabels(np.append(xticks[xticks<bp], [0,sec_lim]).astype(int) )

plt.show()

这篇关于在具有特殊x2和y2轴的matplotlib中制作散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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