I'm trying to graph two lists where my TTS list is a percentage of Volume list. So for example, When TTS is 100, that's a 100% of 15 or whatever value is listed in the same position between the two lists. I'm not sure how to go about this and a novice programmer. I ran into trouble trying to uses lists and do percentages of one another, how should I approach this? Thanks!

import numpy as np
import matplotlib.pyplot as plt

def percentage(percent, whole):
  return (percent * whole) / 100.0

Volume = [0,15,15,15,20,25,30,35,40,50,55,60,65,70,75,80,85,90,95,100]
TTS = [0,100,100,100,100,100,100,100,92,86,83,80,78,77,74,73,72,72,68,65]

plt.xticks([0,10,20,30,40,50,60,70,80,90,100])
plt.yticks([0,10,20,30,40,50,60,70,80,90,100])

plt.plot(TTS, 'c', linewidth=3.0, label='TTS')
plt.plot(Volume, 'r', linewidth=3.0, label='Volume')

plt.legend()
plt.grid(True)
plt.show()
有帮助吗?

解决方案

Do you mean you want to plot the following:

plt.plot([percentage(tts, vol) for vol, tts in zip(Volume, TTS)])

其他提示

Use map:

def percentage(percent, whole):
  return (percent * whole) / 100.0

Volume = [0,15,15,15,20,25,30,35,40,50,55,60,65,70,75,80,85,90,95,100]
TTS = [0,100,100,100,100,100,100,100,92,86,83,80,78,77,74,73,72,72,68,65]

print map(percentage,Volume,TTS)

Output:

[0.0, 15.0, 15.0, 15.0, 20.0, 25.0, 30.0, 35.0, 36.8, 43.0, 45.65, 48.0, 50.7, 53.9, 55.5, 58.4, 61.2, 64.8, 64.6, 65.0]

elements of Volume and TTS passed to percentage function at the corresponding position, return value from percentage is stored as result list. For ex, 100 percentage of 15 is 15, similarly 90 percentage of 72 is 64.8 (3rd last element)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top