绘图虚线中断数据(类似于等高线图)

Mat*_*ias 18 python plot matplotlib line contour

我遇到了一个(希望)简单的问题.我的目标是绘制一条用数据(不仅是文本)中断的虚线.

我将如何想象可能的结果的示例

由于我发现通过linestyle ='dashed'创建了一个虚线,所以任何帮助都可以将数据放在破折号之间.

Matplotlib已经存在类似于标签的东西 - 正如我在轮廓线演示中看到的那样.

Matplotlib轮廓演示

更新:

理查德在评论中提到问题链接非常有用,但不是我通过评论提到的100%.目前,我是这样做的:

line_string2 = '-10 ' + u"\u00b0" +"C"
l, = ax1.plot(T_m10_X_Values,T_m10_Y_Values)
pos = [(T_m10_X_Values[-2]+T_m10_X_Values[-1])/2., (T_m10_Y_Values[-2]+T_m10_Y_Values[-1])/2.]
# transform data points to screen space
xscreen = ax1.transData.transform(zip(T_m10_Y_Values[-2::],T_m10_Y_Values[-2::]))
rot = np.rad2deg(np.arctan2(*np.abs(np.gradient(xscreen)[0][0][::-1])))
ltex = plt.text(pos[0], pos[1], line_string2, size=9, rotation=rot, color='b',ha="center", va="bottom",bbox = dict(ec='1',fc='1', alpha=0.5))
Run Code Online (Sandbox Code Playgroud)

在这里,您可以看到结果的快照.零下20°C没有BBox.

在此输入图像描述

mtd*_*mtd 1

快速而肮脏的答案使用annotate

import matplotlib.pyplot as plt
import numpy as np

x = list(reversed([1.81,1.715,1.78,1.613,1.629,1.714,1.62,1.738,1.495,1.669,1.57,1.877,1.385]))
y = [0.924,0.915,0.914,0.91,0.909,0.905,0.905,0.893,0.886,0.881,0.873,0.873,0.844]

def plot_with_text(x, y, text, text_count=None):
    text_count = (2 * (len(x) / len(text))) if text_count is None else text_count
    fig, ax = plt.subplots(1,1)
    l, = ax.plot(x,y)
    text_size = len(text) * 10
    idx_step = len(x) / text_count
    for idx_num in range(text_count):
        idx = int(idx_num * idx_step)
        text_pos = [x[idx], y[idx]]
        xscreen = ax.transData.transform(zip(x[max(0, idx-1):min(len(x), idx+2)], y[max(0, idx-1):min(len(y), idx+2)]))
        a = np.abs(np.gradient(xscreen)[0][0])
        rot = np.rad2deg(np.arctan2(*a)) - 90
        ax.annotate(text, xy=text_pos, color="r", bbox=dict(ec="1", fc="1", alpha=0.9), rotation=rot, ha="center", va="center")

plot_with_text(x, y, "test")
Run Code Online (Sandbox Code Playgroud)

产量:

阴谋

您可以使用偏移来获得更令人满意的结果。