简体   繁体   English

如何使用 matplotlib 获得 plot 的步进单元?

[英]How to get the step unit of a plot using matplotlib?

 ax.text(x=v+ ?? , y= i+addition, s= str(v), color='black')

I created various plots of type horizontal bar chart, and now I want to add text next to each bar in each plot.At the??我创建了各种类型的水平条形图,现在我想在每个 plot 的每个条形旁边添加文本。在?? I want to add quarter of the step unit of each plot, where I am adding the text.我想添加每个 plot 的步长单位的四分之一,我要在其中添加文本。 So for example if the step unit is 80, then it should be 20. ??例如,如果步长单位是 80,那么它应该是 20。 ?? is the horizontal distance between the bar and the text.是条与文本之间的水平距离。 it can't be constant, because I am basically creating many plots with different scales.它不可能是恒定的,因为我基本上是在创建许多不同比例的地块。 Any suggestions??有什么建议么??

My question is basically if there is a method with which i can just get the step unit of a plot, similar getting the xlim and ylim?我的问题基本上是是否有一种方法可以让我获得 plot 的步进单元,类似于获得 xlim 和 ylim? I am assuming the step unit is automatically calculated by default.我假设默认情况下会自动计算步长单位。

Easy clarification of the question轻松澄清问题

You probably want to use ax.annotate ( doc ) that lets you specify the offset of the text from a point in data space in screen space .您可能希望使用ax.annotate ( doc ) 来指定文本与屏幕空间数据空间中的某个点的偏移量。 For example:例如:

import matplotlib.pyplot as plt
import numpy as np

fig, (ax1, ax2) = plt.subplots(2, constrained_layout=True)

vals = np.arange(5) + 1


def bar_and_label(ax, y, width, text):
    ax.barh(y, width)
    ax.annotate(
        text=text,
        xy=(width[0], 0),
        xytext=(5, 0),
        textcoords="offset points",
        va="center",
        ha="left",
    )


bar_and_label(ax1, range(len(vals)), vals, "a")
bar_and_label(ax2, range(len(vals)), vals * 100, "b")

在此处输入图像描述

This makes sure that the space between the bar and the label is always the same no matter what scale the data is on!这确保了条形图和 label 之间的空间始终相同,无论数据的比例是多少!


You may also be interested in the new in mpl3.4 feature ax.bar_label ( doc ) method.您可能还对 mpl3.4 中的新功能ax.bar_label ( doc ) 方法感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM