Python:在轴系统上创建许多3D圆柱图

时间:2018-07-17 16:55:53

标签: python matplotlib py2neo

美好的一天

我想知道是否有人可以将我引荐到python中能够创建任何类似图的任何库或方法。 (最好是基于MatPlotLib,以便将图形嵌入HTML页面)Prospective plot

这项工作的目的是创建从Neo4J数据库读取的数据的3D渲染并将其建模为下面的圆柱。

1 个答案:

答案 0 :(得分:1)

下面的代码尝试使用数据框中的图例创建类似的3D图(不是圆柱形而是矩形)。该图是交互式的。资源:1234from multiprocessing.dummy import Pool as ThreadPool import Queue,time,random from datetime import datetime class rob: def __init__(self,maxq): self.count = 0 self.q = Queue.Queue(maxq) self.maxq = maxq def add(self,x=1): self.count += x def z(self): while True: #s = random.choice(range(1,6)) #time.sleep(.1) t = datetime.now().strftime("%I:%M:%S.%f") self.add() t = t+'---'+str(self.count) #print 'z:',t if self.q.full(): print 'waiting' self.q.put(t) yield t def do(self,t): secs = random.choice(range(1,10)) #could take max of 10 seconds per do() time.sleep(secs) d = self.q.get() return t #print e.message, e.args #patch_send() mt = 100 b = rob(mt*3) pool = ThreadPool(mt) #because u can results = pool.imap_unordered( b.do, b.z() ) #pool.close() for r in results: print r

导入库

Jupyter Notebook 5.0.0, Python 3.6.6

创建示例数据框

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from mpl_toolkits.mplot3d import axes3d  
import matplotlib.patches as mpatches # for legends
%matplotlib notebook

enter image description here

绘制图形

注意:图形分为两部分:(1)N-S,E-W线的2D图和(2)3D条形图

# Create two sets of identical xpos and ypos 
# So taht the z-values are plotted at same location for stacking
xtemp = np.random.randint(1, 10, size=5)
ytemp = np.random.randint(1, 10, size=5)

df = pd.DataFrame({
    # category
    'season': ['S1']*5 + ['S2']*5 + ['S3']*5,
    #'wins': np.random.randint(1, 10, size=15),
    # define pos
    'xpos' : list(xtemp)+list(xtemp)+list(xtemp),
    'ypos' : list(ytemp)+list(ytemp)+list(ytemp),
    'zpos' : np.zeros(15),
    # define delta
    'dx': 0.8*np.ones(15),
    'dy': 0.8*np.ones(15),
    'dz': np.random.randint(1, 5, size=15), #np.ones(15)

})
df.head(5)

注释线并删除z轴窗格和网格线

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# ..................
# Line-1 on x-y plane
x = [4, 4]
y = [-3, 12]
ax.plot(x, y, zs=0, zdir='z', color='orange', alpha=0.8)


# Line-2 on x-y plane
y = [4, 4]
x = [-3, 12]
ax.plot(x, y, zs=0, zdir='z', color='blue', alpha=0.5)


# Creat multiple overlap plots within a loop

color = ['#6495ED', '#6E8B3D', '#FFB90F']
slist = ['S1', 'S2', 'S3']
stack_zpos = pd.Series(np.zeros(5)) 
for i in range(0,3):
    q = df[df['season']==slist[i]].reset_index(inplace=False)
    ax.bar3d(q.xpos, q.ypos, stack_zpos, q.dx, q.dy, q.dz, color=color[i], alpha=1)
    stack_zpos += q.dz  # values added here for stacking

创建图例并将其添加到图中

# Remove the z-axis panes, grids and lines
alpha = 0
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, alpha))
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, alpha))
#
ax.zaxis._axinfo["grid"]['color'] = (1.0, 1.0, 1.0, alpha)
ax.w_yaxis._axinfo["grid"]['linewidth'] = 0
ax.w_xaxis._axinfo["grid"]['linewidth'] = 0
#
ax.w_zaxis.line.set_lw(0.)
ax.set_zticks([])
#
ax.set_zlabel("") # remove z-axis label 'z'

# ..........
# Annotate the N, S, E, W lines on the x-y plane
zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
xs = (4, 4, -3, 12)
ys = (-3,12, 4, 4)
zs = (0, 0, 0, 0)

i=0 # Counter
nsew = ['N', 'S', 'E', 'W'] # list of labels
for zdir, x, y, z in zip(zdirs, xs, ys, zs):
    label = '{0}'.format(nsew[i])
    #label = 'N, S, E, W' #% (x, y, z, zdir)
    ax.text(x, y, z, label, zdir)
    i +=1 

可视化图

# Add legend
patch1 = mpatches.Patch(color=color[0], label=slist[0])
patch2 = mpatches.Patch(color=color[1], label=slist[1])
patch3 = mpatches.Patch(color=color[2], label=slist[2])

plt.legend(handles=[patch1, patch2,patch3])

enter image description here