簡體   English   中英

在 seaborn 或 matplotlib - python 的 x 軸上打一個扭結

[英]Put a kink in x axis in seaborn or matplotlib - python

我到處搜索,但找不到關於如何在 seaborn/matplotlib 中如何在 x 軸上放置一個扭結的解決方案,如圖所示

在此處輸入圖像描述

以下方法:

  • 隱藏代表現有 x 軸的脊椎
  • 為零添加一個虛擬點(例如在x=40處)
  • 為扭結繪制一些線,使用clip_on=False在主軸區域外繪制,並使用ax.get_xaxis_transform()x使用“數據坐標” ,為y使用“軸坐標”(底部為0 ,底部為1最佳); 從書脊復制粗細和線條顏色
  • 將虛擬零 position 顯示為0
import matplotlib.pyplot as plt
import numpy as np

data = np.random.randint(50, 89, 80)
bins = np.arange(50, data.max()+6, 5)
fig, ax = plt.subplots(figsize=(12,5))

ax.hist(data, bins=bins, fc='skyblue', ec='navy')
false_zero = bins[0] - 10
ax.set_xticks(np.append(false_zero, bins))
ax.set_xticklabels(np.append(0, bins))

for spine in ['top', 'right', 'bottom']:
    ax.spines[spine].set_visible(False)
ax.add_line(plt.Line2D(xdata=[false_zero, false_zero + 2, false_zero + 4, false_zero + 4, false_zero + 6, bins[-1] + 1],
                       ydata=[0, 0, 0.1, -0.1, 0, 0],
                       color=ax.spines['bottom'].get_edgecolor(), lw=ax.spines['bottom'].get_linewidth(),
                       clip_on=False, transform=ax.get_xaxis_transform()))
plt.tight_layout()
plt.show()

x 軸扭結

PS:對於原帖的扭結:

ax.add_line(plt.Line2D(xdata=[false_zero, false_zero + 2, false_zero + 2, false_zero + 6, false_zero + 6, bins[-1] + 1],
                       ydata=[0, 0, 0.1, -0.1, 0, 0], ...))

原始扭結

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM