在Matplotlib中,如何绘制堆积圆柱形柱状图?如果只有一个柱子,又该怎么做?找不到直接的函数来绘制堆积圆柱形柱状图,也没有简单的解决方法。问题在于它既不是真正的2D,也不是真正的3D。使用Matplotlib,需要使2D看起来像3D。这意味着你需要创建一个圆柱形状。为了使它看起来更好,可能还需要一个纹理来产生阴影效果。mplot3d是Matplotlib的3D扩展,可以使用它来绘制圆柱形柱状图。
2、解决方案
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