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

作者:mengwei 时间:2023-11-11 21:01:58 

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

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

演示结果:

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

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

完整代码:


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秒)

来源:https://matplotlib.org/index.html

标签:numpy,matplotlib
0
投稿

猜你喜欢

  • 如何在Python3中使用telnetlib模块连接网络设备

    2022-03-11 12:57:33
  • web前端vue之CSS过渡效果示例

    2024-04-10 10:33:20
  • Pycharm激活码激活两种快速方式(附最新激活码和插件)

    2023-05-17 12:07:07
  • python获取mp3文件信息的方法

    2023-12-18 19:49:11
  • 一篇文章掌握MySQL的索引查询优化技巧

    2024-01-17 22:19:29
  • 在 Pycharm 安装使用black的方法详解

    2023-11-29 12:44:59
  • 基于Python Pygame实现的画饼图游戏

    2023-10-25 18:30:23
  • MySql节点管理安装步骤

    2010-10-14 14:13:00
  • python使用sorted函数对列表进行排序的方法

    2022-08-19 00:36:46
  • Go语言k8s kubernetes使用leader election实现选举

    2024-04-26 17:20:53
  • Firebug 必须掌握的技巧

    2009-12-21 20:04:00
  • Python实现敏感词过滤的4种方法

    2021-10-01 06:21:08
  • sql server 2008 忘记sa密码的解决方法

    2024-01-26 22:48:16
  • 详解Python如何生成词云的方法

    2022-01-12 17:16:37
  • python使用for循环和海龟绘图实现漂亮螺旋线

    2023-08-01 10:38:57
  • python 限制函数调用次数的实例讲解

    2023-11-11 00:34:23
  • Python实现的爬取小说爬虫功能示例

    2022-07-14 20:36:16
  • javascript中的replace函数(带注释demo)

    2024-04-18 09:47:52
  • vue 自定义指令directive的使用场景

    2024-05-13 09:09:08
  • PHP设计模式中的命令模式

    2023-05-27 21:13:43
  • asp之家 网络编程 m.aspxhome.com