matplotlib是否具有在轴坐标中绘制对角线的功能? [英] Does matplotlib have a function for drawing diagonal lines in axis coordinates?

查看:565
本文介绍了matplotlib是否具有在轴坐标中绘制对角线的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplotlib轴具有功能axhlineaxvline,用于分别在给定的y或x坐标上绘制水平或垂直线,而与轴上的数据比例无关.

Matplotlib Axes have the functions axhline and axvline for drawing horizontal or vertical lines at a given y or x coordinate (respectively) independently of the data scale on an Axes.

是否有类似的函数绘制恒定的对角线?例如,如果我有一个具有相似域的变量散点图,了解它们是位于y = x线的上方还是下方通常是有用的:

Is there a similar function for plotting a constant diagonal? For example, if I have a scatterplot of variables with a similar domain, it is often useful to know whether they fall above or below the line of y = x:

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1
f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
ax.plot([-3, 3], [-3, 3], ls="--", c=".3")
ax.set(xlim=(-3, 3), ylim=(-3, 3))

当然可以通过抓住轴限制(ax.get_xlim()等)以编程方式完成此操作,但是a)需要执行一些额外的步骤,并且b)在可能会有更多数据最终显示在图表上的情况下比较脆弱并改变极限. (实际上,在某些情况下,仅添加常数线本身就会拉伸轴).

This can of course be done programmatically by grabbing the axis limits, (ax.get_xlim(), etc.), but that a) takes a few extra steps and b) is brittle in cases where more data might end up on the plot and shift the limits. (Actually in some cases just adding the constant line itself stretches the axes).

最好只是做一个例子,例如ax.axdline(ls="--", c=".3"),但是尚不清楚matplotlib代码库中是否存在类似的东西.我想,您需要做的就是修改axhline代码,以便从[0, 1]y axes 坐标中的[0, 1]中绘制.

It would be preferable to just do, e.g., ax.axdline(ls="--", c=".3"), but it's not clear if something like this exists in the matplotlib codebase. All you would need to do would be modify the axhline code to plot from [0, 1] in axes coordinates for both x and y, I think.

推荐答案

从图的左下角到右上角绘制对角线将通过以下操作完成

Drawing a diagonal from the lower left to the upper right corners of your plot would be accomplished by the following

ax.plot([0, 1], [0, 1], transform=ax.transAxes)

使用transform=ax.transAxes,将提供的xy坐标解释为坐标,而不是 data 坐标.

Using transform=ax.transAxes, the supplied x and y coordinates are interpreted as axes coordinates instead of data coordinates.

这仅是当xy限制相等时的标识行.为了绘制直线y=x以使其始终延伸到绘图的极限,可以使用类似于@Ffisegydd给出的方法,并且可以将其编写为以下函数.

This, as @fqq pointed out, is only the identity line when your x and y limits are equal. To draw the line y=x such that it always extends to the limits of your plot, an approach similar to the one given by @Ffisegydd would work, and can be written as the following function.

def add_identity(axes, *line_args, **line_kwargs):
    identity, = axes.plot([], [], *line_args, **line_kwargs)
    def callback(axes):
        low_x, high_x = axes.get_xlim()
        low_y, high_y = axes.get_ylim()
        low = max(low_x, low_y)
        high = min(high_x, high_y)
        identity.set_data([low, high], [low, high])
    callback(axes)
    axes.callbacks.connect('xlim_changed', callback)
    axes.callbacks.connect('ylim_changed', callback)
    return axes

示例用法:

import numpy as np
import matplotlib.pyplot as plt

mean, cov = [0, 0], [(1, .6), (.6, 1)]
x, y = np.random.multivariate_normal(mean, cov, 100).T
y += x + 1

f, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x, y, c=".3")
add_identity(ax, color='r', ls='--')

plt.show()

这篇关于matplotlib是否具有在轴坐标中绘制对角线的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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