绘制来自两个数据集的值以进行比较 [英] Plotting values from two datasets for comparison

查看:66
本文介绍了绘制来自两个数据集的值以进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制两个数据框以比较结果.我的第一选择是仅根据两个数据框中的一列绘制折线图.

I would like to plot two dataframes in order to compare the results. My first choice would be to plot line charts based only on one column from the two dataframes.

df
       Name Surname P   R   F   
    0   B   N   0.41    0.76    0.53
    1   B   L   0.62    0.67    0.61
    2   B   SV  0.63    0.53    0.52
    3   B   SG  0.43    0.61    0.53
    4   B   R   0.81    0.51    0.53
    5   T   N   0.32    0.82    0.53
    6   T   L   0.58    0.69    0.62
    7   T   SV  0.67    0.61    0.64
    8   T   SG  0.53    0.63    0.57
    9   T   R   0.74    0.48    0.58

data = [['B','N',0.41,0.72,0.51], 
['B','L',0.66,0.67,0.62],
['B','SV',0.63,0.51,0.51],
['B','SG',0.44,0.63,0.51],
['B','R',0.81,0.51,0.62],
['T','N',0.33,0.80,0.47],
['T','L',0.58,0.61,0.63],
['T','SV',0.68,0.61,0.64],
['T','SG',0.53,0.63,0.57],
['T','R',0.74,0.48,0.58]]

df1 = pd.DataFrame(data, columns = ['Name','Surname','P','R','F']) 

我想根据 F 值创建一个图,保留 B/T 和 R、N、L、SV、SG 的信息(在图例/标签中).

I would like to create a plot based on F values, keeping information (in legend/labels) of B/T and R,N,L, SV, SG.

我已经尝试过使用条形图,但这并未考虑标签/图例.

I have tried with bar charts, but this does not take into account labels/legend.

我正在寻找这样的东西:

I am looking for something like this:

fig, ax = plt.subplots()
ax2 = ax.twinx()

df.plot(x="Name", y=["F"], ax=ax)
df1.plot(x="Name", y=["F"], ax=ax2, ls="--")

然而,这遗漏了标签和图例.

However this misses labels and legend.

我也试过:

ax = df.plot()
l = ax.get_lines()
df1.plot(ax=ax, linestyle='--', color=(i.get_color() for i in l))

但我无法通过姓名、姓氏和数据框进行区分(在 x 轴上应该有姓氏).按如下所示分别绘制值(P,R和F)也是可以的:

But I cannot distinguish by Name, Surname and dataframe (on the x axis there should be Surname). It would be also ok to plot separately the values (P, R and F) as follows:

ax = df[['P']].plot()
l = ax.get_lines()
df1[['P']].plot(ax=ax, linestyle='--', color=(i.get_color() for i in l))

我应该根据姓名和姓氏比较两个图的 F 值.任何帮助将不胜感激.

I should compare F values of the two plots based on Name and Surname. Any help would be greatly appreciated.

推荐答案

向图添加有关其他参数的信息的最简单方法是在循环上使用ax.text或ax.annotate之类的函数.代码应如下所示:

The simplest way to add information about other parameters to a graph is to use functions like ax.text or ax.annotate over a loop. The code should look like this:

fig, ax = plt.subplots()
data1 = ax.bar(20*index, df["F"], bar_width)
data2 = ax.bar(20*index+bar_width, df1["F"],bar_width)

for i in index:
    ax.text(i*20-5,0,df['Surname'][i],)
    ax.text(i*20-5,0.05,df['Name'][i])
    ax.text(i*20+bar_width-5,0,df1['Surname'][i])
    ax.text(i*20+bar_width-5,0.05,df1['Name'][i])
plt.show()

有用的链接:Matplotlib 图中文本的官方文档

可能类似的问题:在每个点上的不同文本

编辑 2:没有索引的代码:

fig, ax = plt.subplots()
data1 = ax.plot(df["F"])
data2 = ax.plot(df1["F"])

for i in range(1,10):
    ax.text(i,df["F"][i],df['Name'][i]+" "+df['Surname'][i],)
    ax.text(i,df["F"][i],df['Name'][i]+" "+df['Surname'][i],)
plt.show()

这篇关于绘制来自两个数据集的值以进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆