matplotlib中缺少另一种颜色

时间:2018-01-06 09:48:30

标签: python matplotlib

当我尝试在python中绘制以下数据时,我没有在图中看到绿色部分。请在下面找到它。同时,请注意我使用的是python 2.7.4。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
%matplotlib inline

range = pd.date_range('2015-01-01', '2015-12-31', freq='15min')
df = pd.DataFrame(index = range)
df

# Average speed in miles per hour
df['speed'] = np.random.randint(low=0, high=60, size=len(df.index))
# Distance in miles (speed * 0.5 hours)
df['distance'] = df['speed'] * 0.25 
# Cumulative distance travelled
df['cumulative_distance'] = df.distance.cumsum()

df.head()

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax1.plot(df.index, df['speed'], 'g-')
ax2.plot(df.index, df['distance'], 'b-')

ax1.set_xlabel('Date')
ax1.set_ylabel('Speed', color='g')
ax2.set_ylabel('Distance', color='b')

plt.show()
plt.rcParams['figure.figsize'] = 12,5

enter image description here

1 个答案:

答案 0 :(得分:2)

速度和距离是两个彼此成正比的参数。如果您对速度/距离集进行标准化,则会获得完全相同的图形。当您使用alpha = 1(不透明)绘制草稿时,您看到的唯一颜色是最后绘制的颜色(蓝色)。如果你使用alpha<> 1:

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax1.plot(df.index, df['speed'], 'g-', alpha=0.5)
ax2.plot(df.index, df['distance'], 'b-', alpha=0.1)

ax1.set_xlabel('Date')
ax2.set_ylabel('Distance', color='b')

ax1.set_ylabel('Speed', color='g')

plt.show()
plt.rcParams['figure.figsize'] = 12,5

你看到绿色(实际上是绿色和蓝色的混合物):

enter image description here