python - How to fix alignment of projection from (x,y,z) coordinates onto xy-plane in matplotlib 3d plot? - Stack Overflow

I was trying to make a 3D visualization of the joint probability mass function with the following code:

I was trying to make a 3D visualization of the joint probability mass function with the following code:

import math
import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
    if(1 <= x + y and x + y <= 4):
        return (mathb(3, x) * mathb(2, y) * mathb(3, 4 - x - y)) / mathb(8, 4)
    else:
        return 0.0

x_domain = np.array([0, 1, 2, 3])
y_domain = np.array([0, 1, 2])

X, Y = np.meshgrid(x_domain, y_domain)
X = np.ravel(X)
Y = np.ravel(Y)

Z = np.zeros_like(X, dtype=float)
for i in range(len(X)):
    Z[i] = f(X[i], Y[i])

fig = plt.figure(figsize=(8, 4))
ax = fig.add_subplot(1, 1, 1, projection="3d")

ax.scatter(X, Y, Z) # plots the induvidual points
for i in range(len(X)):  # draws lines from xy plane up (x,y,z)
    ax.plot([X[i], X[i]], [Y[i], Y[i]], [0, Z[i]], color="r")

ax.set_xticks(x_domain)  
ax.set_yticks(y_domain)  
plt.show()

Which gave the following result:

As you can see, the stems of lines from the xy-plane do not align with the integer coordinates. I have searched through the matplotlib documentation and cant find the cause for this (unless I have overlooked something). Does anyone know how to fix the alignment issue?

EDIT It seems stack overflow could not render the mathjax equation, so I removed it.

I was trying to make a 3D visualization of the joint probability mass function with the following code:

import math
import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
    if(1 <= x + y and x + y <= 4):
        return (mathb(3, x) * mathb(2, y) * mathb(3, 4 - x - y)) / mathb(8, 4)
    else:
        return 0.0

x_domain = np.array([0, 1, 2, 3])
y_domain = np.array([0, 1, 2])

X, Y = np.meshgrid(x_domain, y_domain)
X = np.ravel(X)
Y = np.ravel(Y)

Z = np.zeros_like(X, dtype=float)
for i in range(len(X)):
    Z[i] = f(X[i], Y[i])

fig = plt.figure(figsize=(8, 4))
ax = fig.add_subplot(1, 1, 1, projection="3d")

ax.scatter(X, Y, Z) # plots the induvidual points
for i in range(len(X)):  # draws lines from xy plane up (x,y,z)
    ax.plot([X[i], X[i]], [Y[i], Y[i]], [0, Z[i]], color="r")

ax.set_xticks(x_domain)  
ax.set_yticks(y_domain)  
plt.show()

Which gave the following result:

As you can see, the stems of lines from the xy-plane do not align with the integer coordinates. I have searched through the matplotlib documentation and cant find the cause for this (unless I have overlooked something). Does anyone know how to fix the alignment issue?

EDIT It seems stack overflow could not render the mathjax equation, so I removed it.

Share Improve this question asked Jan 31 at 21:53 a_floating_pointa_floating_point 3891 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Actually, they DO align with the integer grid, as you will see by rotating your plot a bit.

It's just that the z axis doesn't naturally start from 0, which is where the end of your whatever-they-are are.

Add the line

ax.set_zlim( 0.0, 0.25 )

just before plt.show() and you will be good to go.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741893351a4372030.html

相关推荐

  • Intel E810ICE DPU RDMA 及MLX中断原理分析2(CEAE)

    接上文: 创建CQ时的comp_vector参数代码语言:javascript代码运行次数:0运行复制struct ibv_cq *ibv_create_cq(struct ibv_context *context, int cqe,voi

    2小时前
    00
  • 我“AI”发文——AI在法律、医疗、金融等领域的应用前景

    我“AI”发文——AI在法律、医疗、金融等领域的应用前景人工智能(AI)正在深刻影响各个行业,其中法律、医疗和金融领域尤为突出。AI技术的快速发展,使其在提高效率、降低成本、优化决策、增强服务质量等方面发挥了重要作用。本文将详细探讨AI在这

    2小时前
    10
  • 鸿蒙开发:事件订阅EventHub

    前言有这样一个案例,页面A跳转到了页面B,页面B又跳转了页面C,在页面C中需要把数据回调至页面A,我们该如何去做?又或者,在一个模块中需要调用另一个模块中的方法,又该如何调用?在很多的场景下,我们都会想到回调的方式,如果层级较多呢?逐级回调

    2小时前
    00
  • 大模型工业场景使用

    使用AI大模型进行图像的识别,优缺点分别为:1.具有很好的泛化能力,很多场景都能够使用;2.速度慢:整体感觉速度太慢,比较吃硬件;以焊缝识别为例,工业电脑将图片传递给大模型,能够对黑色缺陷点进行识别,但是每张图片大概2-3s,整体效率较低。

    2小时前
    10
  • SEM

    在集成电路制造领域,纳米级晶圆缺陷的检测与分类对于后续的根本原因分析及产量提升至关重要。扫描电子显微镜(SEM)图像中存在的复杂背景图案以及缺陷多样化的纹理特征构成了重大挑战。传统方法通常受限于数据不足、标签欠缺和较差的迁移性。在本文中

    2小时前
    00
  • MySQL源码学习系列(二)

    在实际工作中,我们常常会遇到各种各样的数据库问题,比如查询性能低下、数据不一致等。为了更好地诊断和解决这些问题,MySQL 提供了丰富的日志功能,其中 general log 和慢查询日志(slowlog)是两个非常重要的日志类型。Gene

    1小时前
    10
  • Top 10 漏洞不懂?这还不手拿把掐吗?

    OWASP Top 10 漏洞的本质是:开发者偷懒,黑客笑开颜。防御核心:不信任任何用户输入 + 最小权限原则。1. SQL注入(SQL Injection)段子:HR:你叫什么名字?程序员:&#x27;; DROP TABLE

    1小时前
    10
  • Bacula开源备份系统,企业级数据保护的利器!

    大家好,波哥又来给大家推荐好东西啦!欢迎大家在评论区留言评论自己想了解的工具、方向或职业等互联网相关内容,点赞和推荐多的,波哥会优先安排解答!简介Bacula 是一个开源的企业级计算机备份系统,设计用于处理不同系统的网络,自动完成备份任务

    1小时前
    10
  • 凌晨2点重磅消息:奥特曼宣布OpenAI正式支持MCP协议,科技界再掀波澜!

    OpenAI近期动作频频,继向免费用户开放GPT-4o的图像生成功能后,公司再次推出重要更新。最新发布的Agents SDK现已支持行业标准MCP协议(该协议由竞争对手Anthropic主导开发)。OpenAI首席执行官萨姆·奥特曼于凌晨时

    1小时前
    10
  • 傻瓜式复制AI提示词模板,手把手教你用AI搞定复杂Git操作

    前端开发中,你是不是也遇到过下面的场景:Git分支一旦冲突就手忙脚乱?想要清理提交历史却无从下手?对复杂的Git操作充满不确定感?今天,我们提供一个傻瓜式AI提示模板,你只需要简单复制并填写自己的Git问题,AI就会逐步、详细地为你给出精确

    1小时前
    00
  • Java + LangChain 开发大语言模型应用!

    在 Baeldung 上看到了一篇介绍基于 Java + LangChain 开发大语言模型应用的基础入门文章,写的非常不错,非常适合初学者。于是,我抽空翻译了一下。1. 简介在本教程中,我们将详细探讨 LangChain[1],一个用于开

    1小时前
    00
  • C# 自定义异常:提升代码健壮性与可维护性

    一、引言在软件开发过程中,异常处理是保证程序健壮性的重要手段。当程序运行时遇到不符合预期的情况,就会抛出异常。C# 作为一门面向对象的编程语言,提供了丰富的内置异常类,如 ArgumentException、NullReferenceExc

    1小时前
    00
  • 基于 keyed DI 的 HttpClient

    基于 keyed DI 的 HttpClientIntro.NET 8 中依赖注入引入了 keyed service 的支持 可以参考 .NET 8 中的 KeyedService,.NET 9 中改进了 HttpClient 基于名称的

    1小时前
    00
  • AI 写文章系列——Ubuntu 24.04 pipx install 解决 ModuleNotFoundError 问题

    概述自从转 Go 之后,好久没写 Python 了。最近在 Ubuntu 24.04 中使用 pipx 安装了一个工具之后,运行报依赖错误。把上下文喂给 DeepSeek 之后,最终解决了问题。我让 DeepSeek 回顾问答的上下文,整理

    1小时前
    00
  • 3D领域DeepSeek「源神」启动!国产明星创业公司,一口气开源八大项目

    机器之心报道机器之心编辑部2025 开年,DeepSeek-R1 的成功在全球掀起了一股开源风潮,上个月的开源周更是毫无保留地将自己的多项核心技术开放给了全球开发者。这种「完整技术栈」式的开源震撼了整个行业。毫无疑问,开源正成为国内外大模型

    1小时前
    00
  • 开源免费真香!Star 1.4k 这款开源在线教育系统让万人学习零压力,企业培训系统一键搭建神器

    嗨,大家好,我是小华同学,关注我们获得“最新、最全、最优质”开源项目和高效工作学习方法 PlayEdu 是一款基于 SpringBoot3 + Vue3 开发的开源企业培训系统,提供从课程管理、学员管理到考试测评的全流程解决方案。项目采用前

    1小时前
    00
  • 构建两个线程 一个输出随机偶数整数一个输出随机奇数整数

    代码语言:java复制*** 构建两个线程 一个输出随机偶数整数*一个输出随机奇数整数*public static void printRandomNumByTwoThreads(int num){Rando

    58分钟前
    10
  • 800G光模块:AI算力驱动的未来网络核心

    近年来,以ChatGPT为代表的AI大模型的快速崛起,使得全球对算力的需求呈现爆炸式增长。在AI算力集群中,800G光模块凭借更高的传输速率和更低的功耗,为大规模模型训练和推理任务提供高速低延迟的数据互联,成为未来算力网络不可或缺的核心组件

    55分钟前
    10
  • Claude的SVG绘图能力:AI视觉创作的新境界

    在人工智能领域,Claude 不仅以强大的代码生成能力闻名,其 SVG 绘图功能也正在被越来越多用户发掘和赞赏。这项能力展现了 Claude 在视觉创意方面的惊人潜力,为内容创作者提供了一个强大的辅助工具。惊人的视觉理解能力Claude 的

    41分钟前
    00
  • AI提示词(Prompt)设计优化方案

    AI 提示词(Prompt)设计优化方案 | 高效使用 AI 工具嘿,各位技术探索家们!在这个 AI 横行的时代,你是不是常常感觉自己和 AI 之间隔着一层 “神秘面纱”?明明输入了指令,AI 给出的结果却总是差强人意。其实啊,这里面的关键

    27分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信