Python matplotlib:数据坐标中的位置颜色条

2024-05-04

我想通过指定数据坐标中的位置来将颜色条放置在散点图中。 以下是指定图形坐标时它如何工作的示例:

import numpy as np
import matplotlib.pyplot as plt    

#Generate some random data:
a = -2
b = 2
x = (b - a) * np.random.random(50) + a
y = (b - a) * np.random.random(50) + a
z = (b) * np.random.random(50)

#Do a scatter plot
fig = plt.figure()
hdl = plt.scatter(x,y,s=20,c=z,marker='o',vmin=0,vmax=2)
ax = plt.gca()
ax.set_xlim([-2,2])
ax.set_ylim([-2,2])

#Specifying figure coordinates works fine:
fig_coord = [0.2,0.8,0.25,0.05]
cbar_ax = fig.add_axes(fig_coord)

clevs = [0, 1 , 2]
cb1 = plt.colorbar(hdl, cax=cbar_ax, orientation='horizontal', ticks=clevs)

plt.show()

...好吧,这里不能包含情节的图像,因为我缺乏声誉。但上面的代码会给你一个印象......

现在的问题是,如何将颜色条定位在数据坐标处,以显示在例如: 左、下、宽、高:-1.5、1.5、1、0.25

我尝试了一些事情,例如确定图中的轴位置并将其转换为数据坐标,但没有成功。

非常感谢您的想法或指出我已经回答了类似的问题!

这是我所做的(不是特别漂亮,但有帮助)。谢谢tcaswell https://i.stack.imgur.com/fB4hh.png !

#[lower left x, lower left y, upper right x, upper right y] of the desired colorbar:
dat_coord = [-1.5,1.5,-0.5,1.75]
#transform the two points from data coordinates to display coordinates:
tr1 = ax.transData.transform([(dat_coord[0],dat_coord[1]),(dat_coord[2],dat_coord[3])])
#create an inverse transversion from display to figure coordinates:
inv = fig.transFigure.inverted()
tr2 = inv.transform(tr1)
#left, bottom, width, height are obtained like this:
datco = [tr2[0,0], tr2[0,1], tr2[1,0]-tr2[0,0],tr2[1,1]-tr2[0,1]]
#and finally the new colorabar axes at the right position!
cbar_ax = fig.add_axes(datco)
#the rest stays the same:
clevs = [0, 1 , 2]
cb1 = plt.colorbar(hdl, cax=cbar_ax, orientation='horizontal', ticks=clevs)

plt.show()

根据对我原来问题的评论,这是我所做的:

import numpy as np
import matplotlib.pyplot as plt    

a = -2
b = 2

x = (b - a) * np.random.random(50) + a
y = (b - a) * np.random.random(50) + a
z = (b) * np.random.random(50)

fig = plt.figure()
hdl = plt.scatter(x,y,s=20,c=z,marker='o',vmin=0,vmax=2)
ax = plt.gca()
ax.set_xlim([-2,2])
ax.set_ylim([-2,2])

#[(lower left x, lower left y), (upper right x, upper right y)] of the desired colorbar:
dat_coord = [(-1.5,1.5),(-0.5,1.75)]
#transform the two points from data coordinates to display coordinates:
tr1 = ax.transData.transform(dat_coord)
#create an inverse transversion from display to figure coordinates:
inv = fig.transFigure.inverted()
tr2 = inv.transform(tr1)
#left, bottom, width, height are obtained like this:
datco = [tr2[0,0], tr2[0,1], tr2[1,0]-tr2[0,0],tr2[1,1]-tr2[0,1]]
#and finally the new colorabar axes at the right position!
cbar_ax = fig.add_axes(datco)
#the rest stays the same:
clevs = [0, 1 , 2]
cb1 = plt.colorbar(hdl, cax=cbar_ax, orientation='horizontal', ticks=clevs)

plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python matplotlib:数据坐标中的位置颜色条 的相关文章

  • 在 Django 中获取数据库类型[重复]

    这个问题在这里已经有答案了 我需要能够确定 Django 运行时使用的数据库类型 MYSQL False if
  • 顶级棉花糖模式验证

    From 棉花糖 validation http marshmallow readthedocs org en latest quickstart html validation 我知道我可以在架构中的特定字段上注册验证器 如果验证器失败
  • 合并数据框中的值以写入 Excel

    我有一个看起来像的数据框 column1 column2 column3 colum4 column5 1 r n 1 r s 1 r n 2 r s 3 r n 3 2 r n 1 r s 1 r n 4 r s 4 r n 5 3 r
  • 是否可以在 Sphinx 中隐藏 Python 函数参数?

    假设我有以下函数 该函数记录在Numpydoc 风格 https github com numpy numpy blob master doc HOWTO DOCUMENT rst txt 并且文档是自动生成的Sphinx http sph
  • 查找 python 数据框中每行的最高值

    我想找到每行中的最高值并返回 python 中该值的列标题 例如 我想找到每行的前两个 df A B C D 5 9 8 2 4 1 2 3 我希望我的输出看起来像这样 df B C A D 您可以使用字典理解来生成largest n数据帧
  • Python Pandas 从宽到长的格式更改以及列标题拆分

    我有一个包含以下列标题和行示例的表 Subject Test1 Result1 Test1 Result2 Test2 Result1 Test2 Result2 0 John 10 0 5 20 0 3 我想将其改造成 Subject l
  • 如何使用 boto3 从 AWS Cognito 获取经过身份验证的身份响应

    我想使用 boto3 获取访问 AWS 服务的临时凭证 用例是这样的 我的 Cognito 用户池中的用户登录到我的服务器 我希望服务器代码为该用户提供访问其他 AWS 服务的临时凭证 我有一个存储我的用户的 Cognito 用户池 我有一
  • Python pandas:删除字符串中分隔符之后的所有内容

    我有数据框 其中包含例如 vendor a ProductA vendor b ProductA vendor a Productb 我需要删除所有内容 包括 两个 以便我最终得到 vendor a vendor b vendor a 我尝
  • 为什么我不能“string”.print()?

    我的理解print 在 Python 和 Ruby 以及其他语言 中 它是字符串 或其他类型 上的方法 因为它的语法非常常用 打印 嗨 works 那么为什么不呢 hi print 在 Python 中或 hi print在红宝石工作 当你
  • 将分布拟合到直方图

    I want to know the distribution of my data points so first I plotted the histogram of my data My histogram looks like th
  • 使用 python-docx 在 docx 文件中查找所有“正常”样式且字体大小不是 11 的文本

    到目前为止我的实现 from docx api import Document import pandas as pd from docx shared import Pt texts sizes document Document new
  • 将 for 循环替换为 pyspark 中的并行进程

    我在脚本中使用 for 循环来为 size DF 数据帧 的每个元素调用函数 但这需要很多时间 我尝试通过地图删除 for 循环 但没有得到任何输出 size DF 是我从表中获取的大约 300 个元素的列表 用于 import call
  • PyPI 上的轮子平台约束有什么限制吗?

    是否有任何地方 PEP 或其他地方 声明关于 Linux 轮子上传范围的限制 PyPI http pypi io 应该有 具体来说 上传是否被认为是可接受的做法linux x86 64轮子到 PyPI 而不是manylinux1 x86 6
  • Python `concurrent.futures`:根据完成顺序迭代 future

    我想要类似的东西executor map 除了当我迭代结果时 我想根据完成的顺序迭代它们 例如首先完成的工作项应该首先出现在迭代中 等等 这样 当且仅当序列中的每个工作项尚未完成时 迭代就会阻塞 我知道如何使用队列自己实现这一点 但我想知道
  • 在Python中确定句子中2个单词之间的邻近度

    我需要确定 Python 句子中两个单词之间的接近度 例如 在下面的句子中 the foo and the bar is foo bar 我想确定单词之间的距离foo and bar 确定之间出现的单词数foo and bar 请注意 该词
  • Learning_rate 不是合法参数

    我正在尝试通过实现 GridSearchCV 来测试我的模型 但我似乎无法在 GridSearch 中添加学习率和动量作为参数 每当我尝试通过添加这些代码来执行代码时 我都会收到错误 这是我创建的模型 def define model op
  • 让 TensorFlow 在 ARM Mac 上使用 GPU

    我已经安装了TensorFlow在 M1 上 ARM Mac 根据这些说明 https github com apple tensorflow macos issues 153 一切正常 然而 模型训练正在进行CPU 如何将培训切换到GPU
  • 与 GNU Make 等 Python 相关的并行任务并发

    我正在寻找一种方法或者可能是一种哲学方法来如何在 python 中执行类似 GNU Make 的操作 目前 我们使用 makefile 来执行处理 因为 makefile 非常擅长通过更改单个选项 j x 进行并行运行 此外 gnu mak
  • 在Python中打开网站框架或图像

    所以我对 python 相当熟练 并且经常使用 urllib2 和 Cookies 来实现网站自动化 我刚刚偶然发现了 webbrowser 模块 它可以在默认浏览器中打开一个网址 我想知道是否可以从该 url 中仅选择一个对象并打开它 具
  • 使用 Python 进行 Google 搜索网页抓取 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 最近为了工作中的一些项目 学习了很多python 目前我需要使用谷歌搜索结果进行一些网络抓取 我发现几

随机推荐