2013-07-29 2 views
2

matplotlib는 아래처럼 스택 된 막대 모양 막대를 그릴 수 있습니까? 바가 하나 밖에 없으면 어떨까요?matplotlib에 스택 된 원통형 막대 플롯 만들기

그렇지 않은 경우 다른 옵션은 둥근 모서리가있는 막대가있는 일반적인 스태킹 된 막대 그림 일뿐입니다 - 가능합니까?

matplotlib 갤러리와 bar() 설명서를 검색했지만 할 일이 없습니다. 내가 직접 기능을 알지 못한다

enter image description here

답변

5

는 누적 실린더 막대 플롯을 플롯하고 나는 쉬운 해결 방법 중 하나가 생각하지 않습니다. 문제는 실제로 2D 또는 3D가 아닙니다.

Matplotlib을 사용하면 3D와 같은 2D 모양을 만들어야합니다. 이것은 원통형을 만들어야 함을 의미합니다. 모양을 좋게하려면 그림자 모양을 나타 내기 위해 텍스처가 필요할 것입니다.

mplot3d는 Matplotlib의 3D 확장이며 아래 그림을 만들기 위해 사용했습니다. 나는 조금 보이는 것 같아 3D. 윗부분이 약간 왜곡되어 보이고 전체 줄거리가 비스듬히 보입니다 ... mplot3d도 함께 작업하기에 약간의 고통입니다. 원통 모양을 좋게 만드는 데는 상당한 노력이 필요합니다. 코드는 세련되지 않지만 주석을 달았습니다.

Stacked cylinder bar plot with mplot3d

from __future__ import print_function 
from __future__ import division 
from __future__ import absolute_import 

from mpl_toolkits.mplot3d import Axes3D 

import numpy 
import matplotlib 
import matplotlib.pyplot as plt 


def plot_cylinder_element(x, z, dz, rx = 5, ry = 5, color = "b"): 
    """ 
    x: left, right 
    z: start height 
    dz: height of cylinder 
    rx, ry = radius of width (x) and depth (y) 
    color = color 

    Inspired by: 
http://matplotlib.1069221.n5.nabble.com/plot-surface-shading-and-clipping-error-td14031.html 
    """ 

    N = 100    # number of elements 
    # a lower stride will give more faces. A cylinder with 4 faces is a cube :) 
    # I think with N=100 and rstride=2, it will have 50 faces 
    # cstride is the height, rstride the circle 
    cstride_side = 1000 # only 1 element needed 
    rstride_side = 1 # many elements to make a nice cylinder shape 
    cstride_top = 10  
    rstride_top = 10 

    # parameters of cylinder 
    phi = numpy.linspace(0, 2 * numpy.pi, N) 
    _r = numpy.ones(N) 
    _h = numpy.linspace(0, 1, N) 

    # cylinder 
    _x = rx * numpy.outer(numpy.cos(phi), _r) + x 
    _y = ry * numpy.outer(numpy.sin(phi), _r) 
    _z = dz * numpy.outer(numpy.ones(numpy.size(_r)), _h) + z 
    ax.plot_surface(_x, _y, _z, rstride = rstride_side, cstride = cstride_side, linewidth = 0, alpha = 1, color = color) 

    # to cover the gaps between the faces, plot the cylinder again at a slightly smaller radius 
    _x *= 0.99 
    _y *= 0.99 
    ax.plot_surface(_x, _y, _z, rstride = rstride_side + 1, cstride = cstride_side + 1, linewidth=0, alpha=1, color = color) 

    # top 
    _x = rx * numpy.outer(numpy.cos(phi), _h) + x 
    _y = ry * numpy.outer(numpy.sin(phi), _h) 
    _z = numpy.zeros([N,N]) + z + dz + 0.1 
    ax.plot_surface(_x, _y, _z, rstride = rstride_top, cstride = cstride_top, linewidth = 0, alpha = 1, color = color) 

    # plot again with different stride to mask the gaps  
    ax.plot_surface(_x, _y, _z, rstride = rstride_side + 1, cstride = cstride_side + 1, linewidth=0, alpha=1, color = color) 


def plot_cylinder(x, z, rx = 5, ry = 5): 
    """ 
    x: left-right for each cylinder 
    z: list height difference (ie. not cumulative) 
    """ 
    # list with colors 
    colors = ["b", "g", "r", "c", "y", "k"] 
    # plot cylinder elements 
    _z = 0 
    for i in range(len(z)): 
     plot_cylinder_element(x, _z, z[i], rx = rx, ry = ry, color = colors[i % len(colors)]) 
     _z += z[i] 


def cylinder_plot(z, r = 10, dr = 30): 
    """ 
    z: list of different cylinders with for each a list height difference (ie. not cumulative) 
    r: radius 
    dr: distance between cylinders  
    """ 
    # different cylinders next to each other 
    x = numpy.arange(len(z)) * dr 
    # possible difference between width (x) and depth (y) 
    rx = r 
    ry = r 
    # make cylinders 
    for i in range(len(z)): 
     plot_cylinder(x[i], z[i], rx = rx, ry = ry) 


# close earlier plots 
plt.close("all") 

# make figure 
fig = plt.figure() 
ax = Axes3D(fig) 

# set 3D-view 
ax.view_init(elev = 10, azim = 280) 

# make 3 cylinders, with a different number of elements 
cylinder_plot([[5, 10, 5], [3, 5], [1,2,3,4]]) 

# set the labels 
ax.set_xlabel('X') 
ax.set_ylabel('Y') 
ax.set_zlabel('Z') 

# show 
plt.show() 
+0

이 정말 인상적인 작품 ! 아마도 내 응용 프로그램에 너무 많은 번거 로움하지만 노력에 감탄. – TheCodeNovice

관련 문제