2013-10-18 45 views
0

一個加號我想用mapplotlib繪製一個看起來像這樣一個加號:映射與matplotlib

_ 
_| |_ 
|_ _| 
    |_| 

我一直在讀通過matplotlib文檔,但坦率地說,我甚至不能確定搜索什麼來解決我的問題。實際上,我想在同一個X軸上有兩個點(I.E.垂直線),但我似乎無法弄清楚如何做到這一點。理想情況下,我想用一組情節點來做到這一點,但我明白這是不可能的。

請讓我知道,如果我能以任何方式澄清我的問題。

回答

1
  1. 畫出你希望圖形上方格紙,
  2. 寫下x時,角部的y值,
  3. 把這些值轉換成一個對列表,(一個用於x和一個用於y)的,以相同的順序,
  4. 繪製它。

例如:

>>> import matplotlib.pyplot as plt 
>>> fig, ax = plt.subplots() 
>>> y =[10, 20, 20, 30, 30, 40, 40, 30, 30, 20, 20, 10, 10] 
>>> x =[10, 10, 0, 0, 10, 10, 20, 20, 30, 30, 20, 20, 10] 
>>> line, = ax.plot(x, y, 'go-') 
>>> ax.grid() 
>>> ax.axis('equal') 
(0.0, 30.0, 10.0, 40.0) 
>>> plt.show() 

產地:enter image description here

+0

值得一提的是#3的一個簡單方法是使用'zip(* whatever)',例如(1,2,3)和((2,4)]將[(1,2),(2,4),(3,6)]解壓縮成'x,y = zip(*頂點) ,6)'。 – DSM

+0

我可能會補充說,在以後,不喜歡複雜的事情。 –

+0

劇情代碼是什麼?我已經嘗試過'plt.plot(x,'',y,'')'但這看起來並不正確。 – Nanor

0

如果你會做你應該已經發現了幾個環節如何創建自定義標記一點點搜索。我想出的最好回答你的問題是使用Path對象作爲標記。因此,您可以創建創建所需的路徑的功能(我是懶得寫交叉,所以我把一個簡單的矩形):

def getCustomMarker(): 
    verts = [(-1, -1), # left, bottom 
      (-1, 1), # left, top 
      (1, 1), # right, top 
      (1, -1), # right, bottom 
      (-1, -1)] # ignored 

    codes = [matplotlib.path.Path.MOVETO, 
      matplotlib.path.Path.LINETO, 
      matplotlib.path.Path.LINETO, 
      matplotlib.path.Path.LINETO, 
      matplotlib.path.Path.CLOSEPOLY] 

    path = matplotlib.path.Path(verts, codes) 
    return path 

您現在可以與所需的自定義標記來繪製任何數據:

import matplotlib 
import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(0, 2*np.pi, 100) 
y = np.sin(x) 

figure = plt.figure() 
axes = figure.add_subplot(1, 1, 1) 

axes.plot(x, y, marker=getCustomMarker(), markerfacecolor='none', markersize=3) 

plt.show() 

這使您可以將任何標記繪製在您想要的任何位置以達到所需的尺寸。

+0

我很困惑,一個正方形的自定義標記如何解決我的問題? – Nanor