Matplotlib:在对数图中绘制垂直箭头 [英] Matplotlib: Draw a vertical arrow in a log-log plot

查看:89
本文介绍了Matplotlib:在对数图中绘制垂直箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在以下 log-log 图中绘制简单的垂直箭头:

I am not able to draw a simple, vertical arrow in the following log-log plot:

#!/usr/bin/python2

import matplotlib.pyplot as plt
import matplotlib as mpl

plt.yscale('log')
plt.xscale('log')
plt.ylim((1e-20,1e-10))
plt.xlim((1e-12,1))

plt.arrow(0.00006666, 1e-20, 0, 1e-8 - 1e-20, length_includes_head=True)

plt.savefig('test.pdf')

它只是不显示.在文档中,似乎所有参数(如宽度,高度等)都与轴的比例有关.这是非常违反直觉的.我尝试使用axisartist包的twin()在我的顶部定义一个轴,限制(0,1),(0,1)可以更好地控制箭头的参数,但是我无法弄清楚如何在主要轴的顶部拥有一个完全独立的轴.

It just doesn't show. From the documentation it appears as if all the arguments, like width, height and so on relate to the scale of the axis. This is very counter-intuitive. I tried using twin() of the axisartist package to define an axis on top of mine with limits (0,1), (0,1) to have more control over the arrow's parameters, but I couldn't figure out how to have a completely independent axis on top of the primary one.

有什么想法吗?

推荐答案

子图方法

创建子图后,请执行以下操作

After creating the subplots do the following

  • 对齐位置
  • 使用 set_axis_off() 关闭轴(刻度、标签等)
  • 画箭头!
  • Align the positions
  • Use set_axis_off() to turn the axis off (ticks, labels, etc)
  • Draw the arrow!

那么几行就能得到您想要的东西!

So a few lines gets whats you want!

例如

#!/usr/bin/python2

import matplotlib.pyplot as plt

hax = plt.subplot(1,2,1)
plt.yscale('log')
plt.xscale('log')
plt.ylim((1e-20,1e-10))
plt.xlim((1e-12,1))

hax2 = plt.subplot(1,2,2)
plt.arrow(0.1, 1, 0, 1, length_includes_head=True)

hax.set_position([0.1, 0.1, 0.8, 0.8])
hax2.set_position([0.1, 0.1, 0.8, 0.8])

hax2.set_axis_off()

plt.savefig('test.pdf')

重新调整数据

虽然轴标签可能很棘手,但另一种可能更容易的方法是重新缩放数据.

Alternatively a possibly easier approach, though the axis labels may be tricky, is to rescale the data.

import numpy 

# Other import commands and data input

plt.plot(numpy.log10(x), numpy.log10(y))) 

这不是一个很好的解决方案,但如果您可以处理刻度标签,那就是一个不错的结果!

Not a great solution, but a decent result if you can handle the tick labels!

这篇关于Matplotlib:在对数图中绘制垂直箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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