绘制不同日期的分类结果?
Plotting classification results in different dates?
我有一个数据框(my_data)如下:
0 2017-01 2017-02 2017-03 2017-04
0 S1 2 3 2 2
1 S2 2 0 2 0
2 S3 1 0 2 2
3 S4 3 2 2 2
4 … … … … …
5 … … … … …
6 S10 2 2 3 2
此数据框是每个样本 (S1,.., S10) 在不同日期的分类问题的结果。为了简化绘图,我将混淆矩阵转换为不同的数字,如下所示:0 表示“TP”,1 表示“FP”,2 表示“TN”,3 点表示“FN”。现在,我想绘制如下图所示的数据框。
需要说明的是,我已经问过这个问题,但没有人能帮助我。所以,现在我试图让问题更容易理解,以便我可以获得帮助。
遗憾的是,我不知道有什么方法可以用不同的标记绘制一组数据,因此您必须分别绘制所有数据。
您可以使用 matplotlib 绘制数据。我不确定你的数据看起来如何,但是对于包含这些内容的文件:
2017-01,2017-02,2017-03,2017-04
2,3,2,2
2,0,2,0
1,0,2,2
3,2,2,2
2,2,3,2
您可以使用以下代码获取您想要的情节:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
df = pd.read_csv('dataframe.txt', parse_dates = True)
dates = list(df.columns.values) #get dates
number_of_dates = len(dates)
markers = ["o", "d", "^", "s"] #set marker shape
colors = ["g", "r", "m", "y"] #set marker color
# loop over the data in your dataframe
for i in range(df.shape[0]):
# get a row of 1s, 2s, ... as you want your
# data S1, S2, in one line on top of each other
dataY = (i+1)*np.ones(number_of_dates)
# get the data that will specify which marker to use
data = df.loc[i]
# plot dashed line first, setting it underneath markers with zorder
plt.plot(dates, dataY, c="k", linewidth=1, dashes=[6, 2], zorder=1)
# loop over each data point x is the date, y a constant number,
# and data specifies which marker to use
for _x, _y, _data in zip(dates, dataY, data):
plt.scatter(_x, _y, marker=markers[_data], c=colors[_data], s=100, edgecolors="k", linewidths=0.5, zorder=2)
# label your ticks S1, S2, ...
ticklist = list(range(1,df.shape[0]+1))
l2 = [("S%s" % x) for x in ticklist]
ax.set_yticks(ticklist)
ax.set_yticklabels(l2)
labels = ["TP","TN","FP","FN"]
legend_elements = []
for l,c, m in zip(labels, colors, markers):
legend_elements.append(Line2D([0], [0], marker=m, color="w", label=l, markerfacecolor=c, markeredgecolor = "k", markersize=10))
ax.legend(handles=legend_elements, loc='upper right')
plt.show()
从 this answer 中得出的想法。
这导致情节看起来像这样:
编辑 为标记添加了虚线和轮廓,使其看起来更像所讨论的示例。
EDIT2 添加图例。
我有一个数据框(my_data)如下:
0 2017-01 2017-02 2017-03 2017-04
0 S1 2 3 2 2
1 S2 2 0 2 0
2 S3 1 0 2 2
3 S4 3 2 2 2
4 … … … … …
5 … … … … …
6 S10 2 2 3 2
此数据框是每个样本 (S1,.., S10) 在不同日期的分类问题的结果。为了简化绘图,我将混淆矩阵转换为不同的数字,如下所示:0 表示“TP”,1 表示“FP”,2 表示“TN”,3 点表示“FN”。现在,我想绘制如下图所示的数据框。
需要说明的是,我已经问过这个问题,但没有人能帮助我。所以,现在我试图让问题更容易理解,以便我可以获得帮助。
遗憾的是,我不知道有什么方法可以用不同的标记绘制一组数据,因此您必须分别绘制所有数据。
您可以使用 matplotlib 绘制数据。我不确定你的数据看起来如何,但是对于包含这些内容的文件:
2017-01,2017-02,2017-03,2017-04
2,3,2,2
2,0,2,0
1,0,2,2
3,2,2,2
2,2,3,2
您可以使用以下代码获取您想要的情节:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
df = pd.read_csv('dataframe.txt', parse_dates = True)
dates = list(df.columns.values) #get dates
number_of_dates = len(dates)
markers = ["o", "d", "^", "s"] #set marker shape
colors = ["g", "r", "m", "y"] #set marker color
# loop over the data in your dataframe
for i in range(df.shape[0]):
# get a row of 1s, 2s, ... as you want your
# data S1, S2, in one line on top of each other
dataY = (i+1)*np.ones(number_of_dates)
# get the data that will specify which marker to use
data = df.loc[i]
# plot dashed line first, setting it underneath markers with zorder
plt.plot(dates, dataY, c="k", linewidth=1, dashes=[6, 2], zorder=1)
# loop over each data point x is the date, y a constant number,
# and data specifies which marker to use
for _x, _y, _data in zip(dates, dataY, data):
plt.scatter(_x, _y, marker=markers[_data], c=colors[_data], s=100, edgecolors="k", linewidths=0.5, zorder=2)
# label your ticks S1, S2, ...
ticklist = list(range(1,df.shape[0]+1))
l2 = [("S%s" % x) for x in ticklist]
ax.set_yticks(ticklist)
ax.set_yticklabels(l2)
labels = ["TP","TN","FP","FN"]
legend_elements = []
for l,c, m in zip(labels, colors, markers):
legend_elements.append(Line2D([0], [0], marker=m, color="w", label=l, markerfacecolor=c, markeredgecolor = "k", markersize=10))
ax.legend(handles=legend_elements, loc='upper right')
plt.show()
从 this answer 中得出的想法。
这导致情节看起来像这样:
编辑 为标记添加了虚线和轮廓,使其看起来更像所讨论的示例。
EDIT2 添加图例。