簡體   English   中英

Matplotlib:如何 plot 兩個條形圖具有相同的 x/y 軸,但一個沿 y 軸從另一個開始

[英]Matplotlib : How to plot two bar plots with the same x/y axes but one start over the other along y axis

我正在嘗試 plot 兩個條形圖 int 相同的數字。 在下面的代碼中,這些圖一個接一個,因為它們沿 y 軸位於 0 處。

import matplotlib.pyplot as plt
import numpy as np

y1=np.array([1,2,3,4,5])
x1=np.array([1,2,3,2,1])

x2=np.array([1,2,3,2,1])
y2=np.array([6,7,8,9,10])
plt.bar(y1,x1)
plt.bar(y2,x2)

在此處輸入圖像描述

我會橙色條 plot 從坐標開始:(x=6,y=3)。 所以橙色條 plot 應該超過藍色 plot 並在同一個圖中。

只需使用bottom=3提供第二個柱 plot 的底部值

plt.bar(y2,x2, bottom=3)

您還可以使用bottom=max(x2)使其更通用

在此處輸入圖像描述

對於水平條形圖使用left=3

plt.barh(y2,x2,left=3)

在此處輸入圖像描述

暫無
暫無

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

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