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多线程用法实例详解

本文实例分析了python多线程用法。分享给大家供大家参考。具体如下: 今天在学习尝试学习python多线程的时候,突然发现自己一直对super的用法不是很清楚,所以先总结一些遇到的问题...

Python 使用多属性来进行排序

Python 中 list.sort() 是列表中非常常用的排序函数, key 参数可以对单个属性进行排序。 但是想要实现类似 sql 中 order by id, age 一样,对多个...

完美解决Python matplotlib绘图时汉字显示不正常的问题

完美解决Python matplotlib绘图时汉字显示不正常的问题

Matplotlib是一个很好的作图软件,但是python下默认不支持中文,所以需要做一些修改,方法如下: 1.在python安装目录的Lib目录下创建ch.py文件。 文件中代码为:...

Python数据结构与算法之字典树实现方法示例

本文实例讲述了Python数据结构与算法之字典树实现方法。分享给大家供大家参考,具体如下: class TrieTree(): def __init__(self): s...

Python编程实现的简单神经网络算法示例

Python编程实现的简单神经网络算法示例

本文实例讲述了Python编程实现的简单神经网络算法。分享给大家供大家参考,具体如下: python实现二层神经网络 包括输入层和输出层 # -*- coding:utf-8 -*-...