python中通过多个绘图的连续颜色

2024-07-04 16:41:57 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here这是碳(y1985等)与距离(st)的关系图

我试图用颜色绘制一个参数随时间的变化……因此,我希望有一个连续的颜色渐变穿过所有的图,例如代表1985-2018年的浅蓝色到深蓝色。或“jet”彩色地图穿过。。这可能吗

fig, ax = plt.subplots()
ax.scatter(dfc['st'],dfc['y1985'],c='lightskyblue')
ax.scatter(dfc['st'],dfc['y1986'],c='lightskyblue')
ax.scatter(dfc['st'],dfc['y1987'],c='cornflowerblue')
ax.scatter(dfc['st'],dfc['y1988'],c='cornflowerblue')
ax.scatter(dfc['st'],dfc['y1989'],c='steelblue')
ax.scatter(dfc['st'],dfc['y1990'],c='steelblue')
ax.scatter(dfc['st'],dfc['y1991'],c='royalblue')
ax.scatter(dfc['st'],dfc['y1992'],c='royalblue')
ax.scatter(dfc['st'],dfc['y1993'],c='navy')
ax.scatter(dfc['st'],dfc['y1994'],c='navy')
ax.scatter(dfc['st'],dfc['y1995'],c='lightgreen')
ax.scatter(dfc['st'],dfc['y1996'],c='lightgreen')
ax.scatter(dfc['st'],dfc['y1997'],c='mediumseagreen')
ax.scatter(dfc['st'],dfc['y1998'],c='mediumseagreen')
ax.scatter(dfc['st'],dfc['y1999'],c='seagreen')
ax.scatter(dfc['st'],dfc['y2000'],c='seagreen')
ax.scatter(dfc['st'],dfc['y2001'],c='green')
ax.scatter(dfc['st'],dfc['y2002'],c='green')
ax.scatter(dfc['st'],dfc['y2003'],c='darkgreen')
ax.scatter(dfc['st'],dfc['y2004'],c='darkgreen')
ax.scatter(dfc['st'],dfc['y2005'],c='lightsalmon')
ax.scatter(dfc['st'],dfc['y2006'],c='lightsalmon')
ax.scatter(dfc['st'],dfc['y2007'],c='darksalmon')
ax.scatter(dfc['st'],dfc['y2008'],c='darksalmon')
ax.scatter(dfc['st'],dfc['y2009'],c='coral')
ax.scatter(dfc['st'],dfc['y2010'],c='coral')
ax.scatter(dfc['st'],dfc['y2011'],c='orangered')
ax.scatter(dfc['st'],dfc['y2012'],c='orangered')
ax.scatter(dfc['st'],dfc['y2013'],c='maroon')
ax.scatter(dfc['st'],dfc['y2014'],c='maroon')
ax.scatter(dfc['st'],dfc['y2015'],c='mediumpurple')
ax.scatter(dfc['st'],dfc['y2016'],c='mediumpurple')
ax.scatter(dfc['st'],dfc['y2017'],c='rebeccapurple')
ax.scatter(dfc['st'],dfc['y2018'],c='rebeccapurple')

谢谢你的帮助:)


Tags: 颜色greenaxstscatternavydfclightskyblue
2条回答

如果查看this example plot,它会为颜色创建一个随机数组:

...
theta = 2 * np.pi * np.random.rand(N)
colors = theta
...
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)

由于我对你的数据一无所知,我给你一个大致的答案:

import numpy as np

years = ['y' + str(i) for i in range(1985, 2019)]
len_years = len(years)
colors = 2 * np.pi * np.random.rand(len_years)

fig, ax = plt.subplots()
for idx, year in enumerate(years):
  ax.scatter(dfc['st'],dfc[year], c=colors[idx])

希望这有帮助:)

套餐:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

示例数据:

dfc = pd.DataFrame({'st':[1,2,3,4,5], 'y1985':[2,4,6,7,8], 'y1986':[4,5,6,7,10], 'y1987':[7,9,11,12,15]})

数据采用宽格式,对于Matplotlib,长格式更适合。为此,我们可以使用pd.melt

ndfc = pd.melt(dfc, id_vars = ['st'])

然后,我们需要用序列重新编码时间数据:

ndfc['code'] = pd.factorize(ndfc['variable'])[0]

情节:

fig, ax = plt.subplots()
ax.scatter(ndfc['st'],ndfc['value'],c=ndfc['code'], cmap="jet_r")

相关问题 更多 >

    热门问题