2014-09-19 3 views
0

나는 다음과 같은 방법으로 이미지를 생성하기 matplotlib를 사용다각형 위에 원을 그리는 방법은 무엇입니까?

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.fill(border[0],border[1], color='g', linewidth=1, fill=True, alpha = 0.5) 
patches = [] 
for x1,y1,r in zip(x, y, radii): 
    circle = Circle((x1,y1), r) 
    patches.append(circle) 
p = PatchCollection(patches, cmap='cool', alpha=1.0) 
p.set_array(c) 
ax.add_collection(p) 
plt.colorbar(p) 
plt.savefig(fig_name) 

은 내가 가지고 싶은 것은이 다각형의 상단에 (국경에서 발급 해주는) 다각형과 색 원이다. 그러나 원의 꼭대기에는 다각형이 있습니다.

폴리곤을 먼저 플롯 한 다음 플롯에 원을 추가하기 때문에 이것은 이상합니다.

아무도 왜 그런 일이 벌어지고이 문제를 어떻게 해결할 수 있는지 알고 있습니까? 당신은 zorder 찾고있는

import pandas 

import matplotlib 
import matplotlib.pyplot as plt 
from matplotlib.collections import PatchCollection 
from matplotlib.patches import Circle, Polygon 
import numpy as np 

def plot_xyc(df, x_col, y_col, c_col, radius, fig_name, title, zrange): 


    resolution = 50 

    x = df[x_col] 
    y = df[y_col] 
    c = df[c_col] 

    x0 = (max(x) + min(x))/2.0 
    y0 = (max(y) + min(y))/2.0 

    dx = (max(x) - min(x)) 
    dy = (max(y) - min(y)) 

    delta = max(dx, dy) 

    radii = [delta*radius for i in range(len(x))] 

    fig = plt.figure() 
    plt.title(title) 

    ax = fig.add_subplot(111) 


    border = ([-3, 3, 3, -3], [-3, -3, 3, 3]) 

    ax.fill(border[0],border[1], color='g', linewidth=1, fill=True, alpha = 1.0) 

    patches = [] 
    for x1,y1,r in zip(x, y, radii): 
     circle = Circle((x1,y1), r) 
     patches.append(circle) 


    patches.append(Circle((-100,-100), r)) 
    patches.append(Circle((-100,-100), r)) 

    p = PatchCollection(patches, cmap='cool', alpha=1.0) 

    p.set_array(c) 
    max_ind = max(c.index) 
    c.set_value(max_ind + 1, min(zrange)) 
    c.set_value(max_ind + 2, max(zrange)) 


    plt.xlim([x0 - delta/2.0 - 0.05*delta, x0 + delta/2.0 + 0.05*delta]) 
    plt.ylim([y0 - delta/2.0 - 0.05*delta, y0 + delta/2.0 + 0.05*delta]) 

    ax.add_collection(p) 


    plt.colorbar(p) 

    plt.savefig(fig_name) 

if __name__ == '__main__': 

    df = pandas.DataFrame({'x':[1,2,3,4], 'y':[4,3,2,1], 'z':[1,1,2,2]}) 

    plot_xyc(df, 'x', 'y', 'z', 0.1, 'test2.png', 'My Titlle', (0.0, 3.0)) 
+0

완벽하게 작동하는 예를 제공 할 수 있습니까? – Ffisegydd

답변

2

:

여기에 완전히 예를 노력하고, 요청으로

을 추가했습니다.

matplotlib에서 모든 추가 인수는 클래스 계층 구조로 전달됩니다. zorderArtist 클래스의 kwarg이므로 어느 시점에서 zorder이되도록해야합니다.

예제에는 두 가지 방법이 있습니다. 모두

p = PatchCollection(patches, cmap='cool', alpha=1.0, zorder=2) 

또는 당신이 원하는 경우 : 여기

ax.fill(border[0],border[1], color='g', linewidth=1, fill=True, alpha = 1.0, zorder=1) 

또는 :

여기에 추가 중 하나. zorder이 더 높은 개체는 낮은 값을 가진 개체 위에 위치합니다.

관련 문제