Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib散点到matplotlib plot之间的颜色参数是否一致?_Python_Pandas_Matplotlib_Python Xarray - Fatal编程技术网

Python matplotlib散点到matplotlib plot之间的颜色参数是否一致?

Python matplotlib散点到matplotlib plot之间的颜色参数是否一致?,python,pandas,matplotlib,python-xarray,Python,Pandas,Matplotlib,Python Xarray,我希望使用matplotlib来绘制月度数据的年际变化图(如下)。通过在plt.scatter()中传递c=ds['time.year'],我获得了期望的结果。但是,我希望能够使用类似的plt.plot()调用连接这些点。这可能吗 import pandas as pd import matplotlib.pyplot as plt import xarray as xr # create y data y = [] for yr in range(10): for mo in ra

我希望使用matplotlib来绘制月度数据的年际变化图(如下)。通过在
plt.scatter()
中传递
c=ds['time.year']
,我获得了期望的结果。但是,我希望能够使用类似的
plt.plot()
调用连接这些点。这可能吗

import pandas as pd
import matplotlib.pyplot as plt
import xarray as xr

# create y data 
y = []
for yr in range(10):
    for mo in range(12):
        y.append(yr+mo+(yr*mo)**2)

# create datetime vector
t = pd.date_range(start='1/1/2010', periods=120, freq='M')

# combine in DataArray
ds = xr.DataArray(y, coords={'time':t}, dims=['time'])

# scatter plot with color 
im = plt.scatter(ds['time.month'], ds.values, c=ds['time.year'])
plt.colorbar(im)
输出:

我尝试了以下方法,但无效:

plt.plot(ds['time.month'], ds.values, c=ds['time.year'])

您可以创建将年份范围映射到颜色范围的范数。norm和使用的colormap可以作为
ScalarMapple
的输入服务器,以创建附带的颜色条。使用默认的“viridis”颜色映射,代码可能如下所示:

导入matplotlib.pyplot作为plt
从matplotlib.cm导入ScalarMapable
作为pd进口熊猫
将xarray作为xr导入
y=[]
年(10年):
对于范围(12)内的mo:
y、 追加(年+月+(年*月)**2)
t=pd.日期\范围(起始日期为2010年1月1日,周期为120,频率为M)
ds=xr.DataArray(y,coords={'time':t},dims=['time'])
norm=plt.Normalize(ds['time.year'].min(),ds['time.year'].max())
cmap=plt.cm.get_cmap('viridis'))
对于范围内的年份(int(ds['time.year'].min()),int(ds['time.year'].max())+1):
plt.plot(ds['time.month'][ds['time.year']==year],
ds.值[ds['time.year']==year],
ls='-',marker='o',color=cmap(标准(年)))
plt.colorbar(可缩放(cmap=cmap,norm=norm))
plt.xticks(范围(1,13))
plt.show()