2016-11-27 3 views
0

Я пытаюсь сделать довольно простой график рассеяния с ошибками и шкалой полулогий. Что немного отличается от учебников, которые я нашел, так это то, что цвет диаграммы рассеяния должен отслеживать различное количество. С одной стороны, я смог сделать диаграмму рассеяния с ошибками с моими данными, но только с одним цветом. С другой стороны, я понял диаграмму рассеяния с правильными цветами, но без ошибок. Я не могу совместить две разные вещи.диаграмма рассеяния питона с ошибками и цветами, отображающая физическую величину

Вот пример, используя поддельные данные:

#!/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() 

Я даже пытался построить диаграмму рассеяния после errorbar, но по какой-то причине все построенной на том же самом окне помещается в фоновом режиме относительно errorplot. Любые идеи?

Спасибо!

ответ

0

Вы можете установить цвет объекта LineCollection, который был возвращен errorbar, как описано 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 = cb.to_rgba(colors) 

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

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

enter image description here

+0

Это то, что я искал! – andrea

Смежные вопросы