python 带有误差条和颜色的散点图Map物理量

x8diyxa7  于 2023-04-10  发布在  Python
关注(0)|答案(1)|浏览(145)

我试图做一个非常简单的散点图与误差条和semilogy规模.什么是有点不同的教程我发现的是,散点图的颜色应该跟踪不同的数量.一方面,我能够做一个散点图与误差条与我的数据,但只有一种颜色.另一方面,我实现了散点图与正确的颜色,但是没有错误条。我不能把这两个不同的东西结合起来。
下面是一个使用假数据的例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

n=100
Lx_gas = 1e40*np.random.random(n) + 1e37
Tx_gas = np.random.random(n) + 0.5
Lx_plus_error = Lx_gas
Tx_plus_error = Tx_gas/2.
Tx_minus_error = Tx_gas/4.

#actually positive numbers, this is the quantity that should be traced by the
#color, in this example I use random numbers
Lambda = np.random.random(n) 

#this is actually different from zero, but I want to be sure that this simple
#code works with the log axis
Lx_minus_error = np.zeros_like(Lx_gas)

#normalize the color, to be between 0 and 1
colors = np.asarray(Lambda)
colors -= colors.min()
colors *=  (1./colors.max())

#build the error arrays
Lx_error = [Lx_minus_error, Lx_plus_error]
Tx_error = [Tx_minus_error, Tx_plus_error]

##--------------
##important part of the script

##this works, but all the dots are of the same color
#plt.errorbar(Tx_gas, Lx_gas, xerr = Tx_error,yerr = Lx_error,fmt='o')

##this is what is should be in terms of colors, but it is without the error bars
#plt.scatter(Tx_gas, Lx_gas, marker='s', c=colors)

##what I tried (and failed)
plt.errorbar(Tx_gas, Lx_gas, xerr = Tx_error,yerr = Lx_error,\
        color=colors,  fmt='o')

ax = plt.gca()
ax.set_yscale('log')
plt.show()

我甚至试图在错误条之后绘制散点图,但是由于某种原因,在同一窗口上绘制的所有内容都被放在错误图的背景中。
谢谢!

nx7onnlm

nx7onnlm1#

您可以将颜色设置为errorbar返回的LineCollection对象,如here所述。

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

n=100
Lx_gas = 1e40*np.random.random(n) + 1e37
Tx_gas = np.random.random(n) + 0.5
Lx_plus_error = Lx_gas
Tx_plus_error = Tx_gas/2.
Tx_minus_error = Tx_gas/4.

#actually positive numbers, this is the quantity that should be traced by the
#color, in this example I use random numbers
Lambda = np.random.random(n) 

#this is actually different from zero, but I want to be sure that this simple
#code works with the log axis
Lx_minus_error = np.zeros_like(Lx_gas)

#normalize the color, to be between 0 and 1
colors = np.asarray(Lambda)
colors -= colors.min()
colors *=  (1./colors.max())

#build the error arrays
Lx_error = [Lx_minus_error, Lx_plus_error]
Tx_error = [Tx_minus_error, Tx_plus_error]

sct = plt.scatter(Tx_gas, Lx_gas, marker='s', c=colors)
cb = plt.colorbar(sct)

_, __ , errorlinecollection = plt.errorbar(Tx_gas, Lx_gas, xerr = Tx_error,yerr = Lx_error, marker = '', ls = '', zorder = 0)
error_color = sct.to_rgba(colors)

errorlinecollection[0].set_color(error_color)
errorlinecollection[1].set_color(error_color)

ax = plt.gca()
ax.set_yscale('log')
plt.show()

相关问题