2016-12-14 35 views
0

我做了一個水平條形圖,現在我需要在條上添加標記。我該怎麼做?如何在Python中的條形圖上添加標記?

代碼我迄今如下:

enter image description here

def plot_comparison(): 
lengths = [11380, 44547, 166616, 184373, 193068, 258004, 369582, 462795, 503099, 581158, 660724, 671812, 918449] 

y_pos = np.arange(len(length)) 
error = np.random.rand(len(length)) 

plt.barh(y_pos, length, xerr=error, align='center', alpha=0.4) 
plt.yticks(y_pos, length) 
plt.xlabel('Lengths') 
plt.title('Comparison of different cuts') 
plt.show() 
+0

的可能的複製[matplotlib:設置標記單個點上線(http://stackoverflow.com/questions/8409095/matplotlib-set-markers-for-individual-points在線) –

+0

在這種情況下,我需要在條形圖上放置標記。 – Sanchit795

+0

我相信標記僅適用於內襯圖形 –

回答

1

您可以簡單地添加一個plot命令,將y_poslengths作圖。確保指定製造商並將線型設置爲""(或"none"),否則標記將通過直線連接。
下面的代碼可能是你在做什麼。

import matplotlib.pyplot as plt 
import numpy as np 

lengths = [11380, 44547, 166616, 184373, 193068, 258004, 369582, 462795, 503099, 581158, 660724, 671812, 918449] 

y_pos = np.arange(len(lengths)) 
error = np.array(lengths)*0.08 

plt.barh(y_pos, lengths, xerr=error, align='center', alpha=0.4) 
plt.plot(lengths, y_pos, marker="D", linestyle="", alpha=0.8, color="r") 
plt.yticks(y_pos, lengths) 
plt.xlabel('Lengths') 
plt.title('Comparison of different cuts') 
plt.show() 

enter image description here

相關問題