在具有特殊x2和y2轴的matplotlib中制作散点图

编程入门 行业动态 更新时间:2024-10-10 12:25:15
本文介绍了在具有特殊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中制作散点图

本文发布于:2023-11-30 18:19:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:散点图   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!