1

I am trying to change the appearance of a diagram created using plt.errorbar, but I can't get it to change in the ways I would like.

To demonstrate the problem, I have made up some example data:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.axes as axes
import numpy as np
Temps=np.array([18000,15000,14000,12000,11750,11500,10000,5750,6000])
Powers=np.array([1.2,1.0,0.5,.35,0.4,0.2,.15,5.3,4.9])
Errors=100*np.array([2,2,2,2,2,2,2,3,3])

I have a function that turns the temperature values into colours:

def makecolour(t):
    a=(t-min(Temps))/(max(Temps)-min(Temps))
    return [[1-A,0,A] for A in a]

I have also changed some of the other properties of the diagram.

plt.axes(facecolor='black')
plt.yscale('log')
plt.xscale('log')
plt.xlim(2e4,5e3)

plt.errorbar(Temps,Powers,xerr=Errors,ecolor=makecolour(Temps),fmt='.')

I can't get the data points to change colour, only the error bars. When I try to change the colour of the actual points:

plt.errorbar(Temps,Powers,xerr=Errors,ecolor=makecolour(Temps),fmt='.',color=makecolour(Temps))
"Breaks because it fails to interpret the array of colours."

It doesn't work and I'm don't know how to fix it. The closest I have come to a solution is hiding the data points entirely:

plt.errorbar(Temps,Powers,xerr=Errors,ecolor=makecolour(Temps),fmt='.',markersize=0)
"Not showing where the data point is isn't acceptable."`

But this not good enough.

I have also been struggling with the way the axis ticks are displayed when using plt.xscale('log'). Ideally, I want to display the tick labels as a plain integer as opposed to scientific notation, but neither of the solutions I have tried worked. I have tried:

ticker.LogFormatter(base=1)
axes.ticklabel_format(style='plain')

I have searched around on here for previous answers, but I have not found any disussions of similar problems with plt.errorbar. Any help would be much appreciated.

1 Answers1

0

Here is a partial answer. Just first plot without markers and on the same plot without the errorlines.

About the tickers, this post proposes:

ax=plt.gca()
ax.xaxis.set_minor_formatter(ticker.ScalarFormatter())
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())

Demo code:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.axes as axes
import numpy as np

Temps=np.array([18000,15000,14000,12000,11750,11500,10000,5750,6000])
Powers=np.array([1.2,1.0,0.5,.35,0.4,0.2,.15,5.3,4.9])
Errors=100*np.array([2,2,2,2,2,2,2,3,3])

def makecolour(t):
    a=(t-min(Temps))/(max(Temps)-min(Temps))
    return [[1-A,0,A] for A in a]

plt.axes(facecolor='black')
plt.yscale('log')
plt.xscale('log')
plt.xlim(2e4,5e3)

ax=plt.gca()
ax.xaxis.set_minor_formatter(ticker.ScalarFormatter())
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())

plt.errorbar(Temps,Powers,xerr=Errors,ecolor=makecolour(Temps),fmt='.',markersize=0)
plt.errorbar(Temps,Powers,xerr=None,fmt='.')

plt.show()

resulting plot

JohanC
  • 71,591
  • 8
  • 33
  • 66