Matplotlib更改比例以记录更改线条颜色

时间:2014-11-05 13:38:11

标签: python matplotlib

非常自我解释,在我的answer to this中,当我将x轴放在对数刻度上时,线条的颜色会发生变化。

经过一些进一步的调查,它只发生在我使用的一个系统上,而不是其他系统。

它发生在Scientific Linux 6.4上,matplotlib版本为1.2.0。它使用MATE桌面和matplotlib版本1.1.1rc在Mint Linux上按预期工作。

创建绘图并显示后,它看起来很好。但是,如果我点击将比例更改为对数(在x或y上),它会改变颜色。

这里颜色很好: enter image description here

但是在改变它之后,它们就被改变了。 enter image description here

知道为什么会这样,或者如何预防?

使用此代码:

import matplotlib.pyplot as pyplot
from math import floor, log10
import cProfile
import sys
import numpy as np
from scipy import interpolate


def digit(number):
  if number >= 10000:
    return int(floor(number / 10000))
  if number >= 1000:
    return int(floor(number / 1000))
  elif number >= 100:
    return int(floor(number / 100))
  elif number >= 10:
    return int(floor(number / 10))
  else:
    return number

def digit2(number):
  return int(str(number)[0])


def calcProbs(startingN = 10, maxN = 100, logScale = True, interp = False):


  if logScale:
    lowPower, highPower = int(floor(log10(startingN))), int(floor(log10(maxN)))
    Ns = [i * 10 ** j for j in range(lowPower, highPower) for i in range(1,10)] + [10 ** (highPower)]
    probs = [[0 for _ in range(10)] for _ in range(len(Ns))]
  else:
    Ns = range(startingN, maxN+1)
    probs = [[0 for _ in range(10)] for _ in range(maxN-startingN+1)]

  for N, prob in zip(Ns, probs):
    print N, prob

  for index, N in enumerate(Ns):
    print N
    for n in range(1,N+1):
      for i in range(1, n+1):
        probs[index][digit(i)-1] += 1.0 / (N  * n) 

    probs[index][9] = sum(probs[index][:-1]) #Make sure always sums to 1.

  probs = zip(*probs)

  if interp:
    newNs = range(min(Ns), max(Ns))
    for i, prob in enumerate(probs):
      probs[i] = interpolate.splev(newNs, interpolate.splrep(Ns, prob,s=0), der=0)

    Ns = newNs

  return Ns, probs

"""
cProfile.run("calcProbs(maxN=500)")

"""
Ns, probs = calcProbs(maxN=100, logScale=False, interp=True)


fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)

for prob, benford in zip(probs, [0.301, 0.176, 0.125, 0.097, 0.079, 0.067, 0.085, 0.051, 0.046]):
  line = ax.plot(Ns, prob)
  ax.plot(Ns, [benford for _ in Ns], ls="--", color=line[0]._color)


#ax.legend()
pyplot.show()

虽然我怀疑这可能是系统/安装相关的问题。

0 个答案:

没有答案