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

yipeiwu_com6年前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实现在不同平面的二维条形图的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

使用rst2pdf实现将sphinx生成PDF

使用rst2pdf实现将sphinx生成PDF

当初项目文档是用sphinx写的,一套rst下来make html得到一整个漂亮的在线文档。现在想要将文档导出为离线的handbook pdf,于是找到了rst2pdf这个项目,作为sp...

python多进程使用及线程池的使用方法代码详解

多进程:主要运行multiprocessing模块 import os,time import sys from multiprocessing import Process cla...

Python 批量刷博客园访问量脚本过程解析

Python 批量刷博客园访问量脚本过程解析

今早无聊。。。7点起来突然想写个刷访问量的。。那就动手吧 仅供测试,不建议刷访问量哦~~ 很简单的思路,第一步提取代理ip,第二步模拟访问。 提取HTTP代理IP 网上很多收费的代理和...

巧用Python装饰器 免去调用父类构造函数的麻烦

先看一段代码: 复制代码 代码如下: class T1(threading.Thread): def __init__(self, a, b, c): super(T1, self)._...

编写Python脚本来获取mp3文件tag信息的教程

下面利用一个python的实例程序,来学习python。这个程序的目的就是分析出所有MP3文件的Tag信息并输出。 import os # 导入os模块,提供文件路径,列出文件等方法 i...