Python Matplotlib轴限制和文本位置独立于数据集单位

Python Matplotlib轴限制和文本位置独立于数据集单位,python,matplotlib,Python,Matplotlib,尽管来自不同的数据集,但我试图以相同的方式制作格式相同的绘图,我遇到了获取一致文本位置和适当轴限制的问题,因为数据集的缩放不完全相同。例如-假设我生成以下立面纵断面: import matplotlib.pyplot as plt import numpy as np Distance=np.array([1000,3000,7000,15000,20000]) Elevation=np.array([100,200,350,800,400]) def MyPlot(X,Y): fi

尽管来自不同的数据集,但我试图以相同的方式制作格式相同的绘图,我遇到了获取一致文本位置和适当轴限制的问题,因为数据集的缩放不完全相同。例如-假设我生成以下立面纵断面:

import matplotlib.pyplot as plt
import numpy as np

Distance=np.array([1000,3000,7000,15000,20000])
Elevation=np.array([100,200,350,800,400])

def MyPlot(X,Y):
    fig = plt.figure()
    ax = fig.add_subplot(111, aspect='equal')
    ax.plot(X,Y)
    fig.set_size_inches(fig.get_size_inches()*2)
    ax.set_ylim(min(Y)-50, max(Y)+500)
    ax.set_xlim(min(X)-50, max(X)+50)

    MaxPoint=X[np.argmax(Y)], max(Y)
    ax.scatter(MaxPoint[0], MaxPoint[1], s=10)
    ax.text(MaxPoint[0], MaxPoint[1]+100, s='Maximum = '+str(MaxPoint[1]), fontsize=8)

MyPlot(Distance,Elevation)  

然后我有另一个数据集,它的缩放方式不同:

Distance2=Distance*4
Elevation2=Elevation*5
MyPlot(Distance2,Elevation2)][2]][2]


由于第一个数据集中的单位变化比第二个数据集中的单位变化大得多,因此文本和轴标签的格式与第二个图中的格式不同。是否有方法调整文本位置和轴限制以适应数据集的相对比例?

可能是您应该在没有任何边框的情况下使用
seaborn
。我认为这是一个很好的方法。 它将是这样的:


您应该在脚本的开头编写字符串
import seaborn

首先,对于这样的偏移量放置文本,您几乎不想使用
text
。相反,请使用
注释
。其优点是可以以点而不是数据单位给出文本的偏移量

接下来,要降低滴答声位置的密度,请使用
ax.locator_params
并更改
nbins
参数
nbins
控制滴答声密度。勾号位置仍将自动选择,但减少
nbins
将减少勾号位置的最大数量。如果选择较低的
nbins
,则可能还需要更改matplotlib在选择勾选间隔时认为“偶数”的数字。这样,您就有了更多的选项来获得预期的刻度数

<>最后,为了避免手动设置一个设置填充的限制,考虑使用<代码>边距(某个百分比)< /C> >按当前限制的百分比填充扩展。 要显示所有项目的完整示例,请执行以下操作:

import matplotlib.pyplot as plt
import numpy as np

distance=np.array([1000,3000,7000,15000,20000])
elevation=np.array([100,200,350,800,400])

def plot(x, y):
    fig, ax = plt.subplots(figsize=(8, 2))

    # Plot your data and place a marker at the peak location
    maxpoint=x[np.argmax(y)], max(y)
    ax.scatter(maxpoint[0], maxpoint[1], s=10)
    ax.plot(x, y)

    # Reduce the maximum number of ticks and give matplotlib more flexibility
    # in the tick intervals it can choose.
    # Essentially, this will more or less always have two ticks on the y-axis
    # and 4 on the x-axis
    ax.locator_params(axis='y', nbins=3, steps=range(1, 11))
    ax.locator_params(axis='x', nbins=5, steps=range(1, 11))

    # Annotate the peak location. The text will always be 5 points from the
    # data location.
    ax.annotate('Maximum = {:0.0f}'.format(maxpoint[1]), size=8,
                xy=maxpoint, xytext=(5, 5), textcoords='offset points')

    # Give ourselves lots of padding on the y-axis, less on the x
    ax.margins(x=0.01, y=0.3)
    ax.set_ylim(bottom=y.min())

    # Set the aspect of the plot to be equal and add some x/y labels
    ax.set(xlabel='Distance', ylabel='Elevation', aspect=1)
    plt.show()

plot(distance,elevation)

如果我们改变数据:

plot(distance * 4, elevation * 5)

最后,您可以考虑将注释放置在轴的顶部,而不是从点偏移:

ax.annotate('Maximum = {:0.0f}'.format(maxpoint[1]), ha='center',
            size=8, xy=(maxpoint[0], 1), xytext=(0, 5),
            textcoords='offset points',
            xycoords=('data', 'axes fraction'))

谢谢,这非常有用。仅供参考,这是刚刚合并到master的,它应该有助于解决蜱虫密度问题(将在2.0版中)