Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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和pandas的半年彩色条_Python_Pandas_Matplotlib_Datetimeindex - Fatal编程技术网

Python matplotlib和pandas的半年彩色条

Python matplotlib和pandas的半年彩色条,python,pandas,matplotlib,datetimeindex,Python,Pandas,Matplotlib,Datetimeindex,我有一个熊猫数据框。我正在绘制散点图,并试图根据颜色条对数据进行分类。我这样做是为了每月分类和质量分类,如下面的示例代码所示 a = np.random.rand(366) b = np.random.rand(366)*0.4 index = (pd.date_range(pd.to_datetime('01-01-2000'), periods=366)) df = pd.DataFrame({'a':a,'b':b},index = index) plt.scatter(df['a'],d

我有一个熊猫数据框。我正在绘制散点图,并试图根据颜色条对数据进行分类。我这样做是为了每月分类和质量分类,如下面的示例代码所示

a = np.random.rand(366)
b = np.random.rand(366)*0.4
index = (pd.date_range(pd.to_datetime('01-01-2000'), periods=366))
df = pd.DataFrame({'a':a,'b':b},index = index)
plt.scatter(df['a'],df['b'],c = df.index.month)
plt.colorbar()

质量方面:

plt.scatter(df['a'],df['b'],c = df.index.quarter)
plt.colorbar()

我的问题是:有没有办法按半年分类。例如,从第1-6个月和第7-12个月开始,也可以按月份进行,如:10-3和4-9
谢谢,我们将非常感谢您的帮助/建议

创建一个自定义函数,将其放入“散射函数到颜色”参数中。我举了一个半年制的例子。您可以将其用作自己拆分函数的模板:

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

# if month is 1 to 6 then the first halfyear else the second halfyear 
def halfyear(m):
    return 0 if (m <= 6) else 1
# vectorize function to use with Series
hy = np.vectorize(halfyear)

a = np.random.rand(366)
b = np.random.rand(366)*0.4
index = (pd.date_range(pd.to_datetime('01-01-2000'), periods=366))
df = pd.DataFrame({'a':a,'b':b},index = index)

# apply custom function 'hy' for 'c' argument
plt.scatter(df['a'],df['b'], c = hy(df.index.month))
plt.colorbar()

plt.show()
将numpy导入为np
作为pd进口熊猫
将matplotlib.pylab作为plt导入
#如果月份为1到6,则为上半年,否则为下半年
def半年(m):
如果(m 0和m月<7)返回0,否则返回1))

我会选择一种不会完全截断每月信息的解决方案。使用相似但可区分月份的颜色,可以按半年和月份进行视觉分类

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

a = np.random.rand(366)
b = np.random.rand(366)*0.4
index = (pd.date_range(pd.to_datetime('01-01-2000'), periods=366))
df = pd.DataFrame({'a':a,'b':b},index = index)

colors=["crimson", "orange", "darkblue", "skyblue"]
cdic = list(zip([0,.499,.5,1],colors))
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("name", cdic,12 )
norm = matplotlib.colors.BoundaryNorm(np.arange(13)+.5,12)

plt.scatter(df['a'],df['b'],c = df.index.month, cmap=cmap, norm=norm)
plt.colorbar(ticks=np.arange(1,13))

plt.show()

那么如何将两个绘图合并到同一个图形中?第二或第三示例
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors

a = np.random.rand(366)
b = np.random.rand(366)*0.4
index = (pd.date_range(pd.to_datetime('01-01-2000'), periods=366))
df = pd.DataFrame({'a':a,'b':b},index = index)

colors=["crimson", "orange", "darkblue", "skyblue"]
cdic = list(zip([0,.499,.5,1],colors))
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("name", cdic,12 )
norm = matplotlib.colors.BoundaryNorm(np.arange(13)+.5,12)

plt.scatter(df['a'],df['b'],c = df.index.month, cmap=cmap, norm=norm)
plt.colorbar(ticks=np.arange(1,13))

plt.show()