【问题标题】:Change color line in plt.plot更改 plt.plot 中的颜色线
【发布时间】:2021-04-27 18:47:53
【问题描述】:

我正在分析 3 种资产的投资组合,并绘制了它们的每日回报,并希望更改这些线的颜色以与其他图表保持一致。

returns = data_v.pct_change()

plt.figure(figsize =(15,7))
for c in data_v.columns.values:
  plt.plot(returns.index, returns[c], lw =2, alpha=0.8, label =c)
plt.legend(loc='upper right', fontsize=12)
plt.ylabel('daily returns')

我想要 IUS4.DE:蓝色 DJSC.MI:绿色 比特币:红色

你知道我该怎么做吗?

数据:https://1drv.ms/u/s!Al_hco83mKKPhi_SPVFg3NIeURA9?e=CDlGnV

【问题讨论】:

  • 请将代码发布为代码(不是图片),并提供示例数据以供使用。
  • 我做到了,谢谢你提醒我。

标签: python matplotlib


【解决方案1】:

您可以使用plot 的颜色参数更改颜色。这个例子预先定义了所有的颜色:

colors = {'IUS4.DE': 'blue', 'DJSC.MI': 'green', 'BTC': 'red'}

plt.figure(figsize =(15,7))
for c in data_v.columns.values:
    plt.plot(returns.index, returns[c], lw =2, alpha=0.8, label =c, color=colors[c])
plt.legend(loc='upper right', fontsize=12)
plt.ylabel('daily returns')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    相关资源
    最近更新 更多