我正在尝试绘制在两个y轴上具有不同范围的多个特征。每个轴可能包含多个特征。下面的代码片段包括对象"Prin Balance“,它是一个df,其中包含按日期索引的数据类型float。“欠款状态”是一个包含Prin余额列标题子集的列表。
Delinquent_States = ['1 Mos','2 Mos','3 Mos','> 3 Mos']
fig, ax = plt.subplots()
plt.plot(Prin_Balances['UPB'], '--r', label='UPB')
plt.legend()
ax.tick_params('Bal', colors='r')
# Get second axis
ax2 = ax.twinx()
plt.plot(Prin_Balances[Delinquent_States], label=Delinquent_States)
plt.legend()
ax.tick_params('vals', colors='b')我的输出需要清理,尤指图例。

欢迎任何建议。
发布于 2017-07-18 08:31:49
就像这样简单:
import pandas
import matplotlib.pyplot as plt
import random
# Generate some random data
df = pandas.DataFrame({'a': [random.uniform(0,0.05) for i in range(15)],
'b': [random.uniform(0,0.05) for i in range(15)],
'c': [random.uniform(0.8,1) for i in range(15)],
'd': [random.uniform(0.8, 1) for i in range(15)],
'e': [random.uniform(0.8, 1) for i in range(15)]})
plt.plot(df)返回:

我建议将它们分开绘制:
fig, ax = plt.subplots(nrows=2,ncols=1)
plt.subplot(2,1,1)
plt.plot(df['a'], 'r', label='Line a')
plt.legend()
plt.subplot(2,1,2)
plt.plot(df['b'], 'b', label='Line b')
plt.legend()其中包括:

添加:
您可以为绘图的每一侧设置不同的比例:
fig, ax = plt.subplots()
plt.plot(df['a'], '--r', label='Line a')
plt.plot(df['b'], '--k', label='Line b')
plt.legend()
ax.tick_params('vals', colors='r')
# Get second axis
ax2 = ax.twinx()
plt.plot(df['c'], '--b', label='Line c')
plt.plot(df['d'], '--g', label='Line d')
plt.plot(df['e'], '--c', label='Line e')
plt.legend()
ax.tick_params('vals', colors='b')不是最漂亮的,但你明白我的意思。

https://stackoverflow.com/questions/45155510
复制相似问题