pyplot:在轴上添加点投影 [英] pyplot: adding point projections on axis

查看:75
本文介绍了pyplot:在轴上添加点投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用plot函数绘制图形.但我想强调一些特殊"的建议.通过在轴上绘制投影并在点和轴上都放置文本来确定点.

类似这样的事情:

我尝试过:

 将matplotlib.pyplot导入为plt[...]plt.plot(X,Y,label ='data')#绘制曲线,X和Y是数组plt.plot(Xp,Yp,c,持续时间),marker ='o')#绘制点@(Xp,Yp),Xp和Yp是标量plt.vlines(Xp,min(Y),Yp,linestyles ='虚线')plt.hlines(Yp,min(X),Xp,linestyles ='dashed')plt.grid(真)plt.show() 

但是我得到的不满意:

获得我想要的东西的正确方法是什么?
我也考虑过 annotate ,但是它似乎并没有满足我的需要.如果我错了,请纠正我.

解决方案

您可以将 annotate

这并不完美,因为您可能仍要手动调整轴坐标(例如, -0.05 而不是 0 )以将标签设置得离轴.

I can get a graph drawn using the plot function. But I would like to highlight some "special" points by having the projections drawn on the axes and putting text on both the point and the axes.

Something like this:

I tried with this:

import matplotlib.pyplot as plt

[...]
plt.plot(X, Y, label='data')  # draw curve, X and Y are arrays
plt.plot(Xp, Yp, c, duration), marker='o') # draw point @(Xp, Yp), Xp and Yp are scalars
plt.vlines(Xp, min(Y), Yp, linestyles='dashed')
plt.hlines(Yp, min(X), Xp, linestyles='dashed')
plt.grid(True)
plt.show()

but what I get is not satisfactory:

What is the right way to get what I want?
I've also considered annotate, but it doesn't seem to do what I need. Correct me if I'm wrong.

解决方案

You can use annotate with a blended transformation:

import matplotlib.pyplot as plt

plt.plot([1,2], [2,4], label='data')
plt.plot([1.7], [3.4], marker='o')
plt.grid(True)

x,y = 1.7, 3.4
arrowprops={'arrowstyle': '-', 'ls':'--'}
plt.annotate(str(x), xy=(x,y), xytext=(x, 0), 
             textcoords=plt.gca().get_xaxis_transform(),
             arrowprops=arrowprops,
             va='top', ha='center')
plt.annotate(str(y), xy=(x,y), xytext=(0, y), 
             textcoords=plt.gca().get_yaxis_transform(),
             arrowprops=arrowprops,
             va='center', ha='right')

It's not perfect as the you'll still may want to manually adjust the axis coordinates (e.g. -0.05 instead of 0) to set the labels a bit off the axes.

这篇关于pyplot:在轴上添加点投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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