Python+matplotlib+numpy实现在不同平面的二维条形图

yipeiwu_com5年前Python基础

在不同平面上绘制二维条形图。

本实例制作了一个3d图,其中有二维条形图投射到平面y=0,y=1,等。

演示结果:

完整代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)


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

colors = ['r', 'g', 'b', 'y']
yticks = [3, 2, 1, 0]
for c, k in zip(colors, yticks):
  # Generate the random data for the y=k 'layer'.
  xs = np.arange(20)
  ys = np.random.rand(20)

  # You can provide either a single color or an array with the same length as
  # xs and ys. To demonstrate this, we color the first bar of each set cyan.
  cs = [c] * len(xs)
  cs[0] = 'c'

  # Plot the bar graph given by xs and ys on the plane y=k with 80% opacity.
  ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# On the y axis let's only label the discrete values that we have data for.
ax.set_yticks(yticks)

plt.show()

脚本运行时间:(0分0.063秒)

总结

以上就是本文关于Python+matplotlib+numpy实现在不同平面的二维条形图的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

Python极简代码实现杨辉三角示例代码

杨辉三角,又称贾宪三角形,帕斯卡三角形,是二项式系数在三角形中的一种几何排列。 把每一行看做一个list,写一个generator,不断输出下一行的list 实现下列输出效果: #...

pandas.read_csv参数详解(小结)

pandas.read_csv参数整理  读取CSV(逗号分割)文件到DataFrame 也支持文件的部分导入和选择迭代 更多帮助参见:http://pandas.pydata...

Python实现字典按key或者value进行排序操作示例【sorted】

本文实例讲述了Python实现字典按key或者value进行排序操作。分享给大家供大家参考,具体如下: 要点:使用到了python的内建函数与lambda函数 代码如下:(可直接复制运行...

python del()函数用法

示例程序如下: >>> a = [-1, 3, 'aa', 85] # 定义一个list>>> a[-1, 3, 'aa', 85]>>...

PyQt5每天必学之进度条效果

PyQt5每天必学之进度条效果

进度条是,当我们处理冗长的任务时使用的控件。它是以动画的形式让用户知道该任务正在取得进展。该QProgressBar控件提供一个水平或垂直进度条。程序员可以设置进度条的最小值和最大值。默...