使用基于另一个数据框的标记绘制数据框
Plot dataframe using markers based on another dataframe
我正在尝试将 df2
绘制为常规连续图,但使用从 df1
到 select 标记的值。
DATAdict = {
'A': [ "foo", "miau", "ciao" ],
'B': [ "miau", "haha", "ciao"],
}
df1 = pd.DataFrame.from_dict(DATAdict, orient='columns')
DATAdict = {
'A': [ 0.79, 0.86, 0.88, ],
'B': [ 0.89, 0.89, 0.90, ],
}
df2 = pd.DataFrame.from_dict(DATAdict, orient='columns')
unique = pd.unique(df1.values.ravel('K'))
markers = {}
marker_list = ["s", "o", "+", "x", "P"]
for label, i in zip(unique, range(0, len(unique))):
markers[label] = marker_list[i]
想法是绘制 A 列,使用 range(0,3)
作为 x 轴,使用 0.79、0.86 等作为 y 轴,然后 select 基于 foo、miau 和 ciao 的标记。
最简单的方法是使用 sns.scatterplot
和 style
参数:
style
: vector or key in data
Grouping variable that will produce points with different markers. Can have a numeric dtype but will always be treated as categorical.
要连接线,我不知道有什么方法可以用 sns.scatterplot
做到这一点,但我们可以使用 Axes 句柄覆盖线:
import seaborn as sns
ax = sns.scatterplot(data=df2.reset_index(), x='index', y='A', style=df1['A'], s=100)
ax.plot(df2.index, df2['A'])
请注意,seaborn 会自动选择 style
标记。您可以使用 markers
参数手动设置它们, 但是 标记不能混合 filled and unfilled markers:
marker_list = ['s', 'o', 'P', 'X'] # all "filled" type
ax = sns.scatterplot(data=df2.reset_index(), x='index', y='A', style=df1['A'], markers=marker_list, s=100)
我在 matplotlib 中找不到不单独绘制散点标记的方法:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
DATAdict = {
'A': [ "foo", "miau", "ciao" ],
'B': [ "miau", "haha", "ciao"],
}
df1 = pd.DataFrame.from_dict(DATAdict, orient='columns')
DATAdict = {
'A': [ 0.79, 0.86, 0.88, ],
'B': [ 0.89, 0.89, 0.90, ],
}
df2 = pd.DataFrame.from_dict(DATAdict, orient='columns')
marker_list = ["s", "o", "+", "x", "P"]
marker_dict = dict(zip(pd.unique(df1.values.flat), marker_list))
ax = df2.plot()
row, col = df1.shape
for x, y, m, c in zip(np.repeat(np.arange(row), col), df2.values.flat, df1.values.flat, np.tile(np.arange(col), row)):
if not c:
ax.set_prop_cycle(None)
ax.scatter(x, y, marker=marker_dict[m])
plt.show()
示例输出:
我正在尝试将 df2
绘制为常规连续图,但使用从 df1
到 select 标记的值。
DATAdict = {
'A': [ "foo", "miau", "ciao" ],
'B': [ "miau", "haha", "ciao"],
}
df1 = pd.DataFrame.from_dict(DATAdict, orient='columns')
DATAdict = {
'A': [ 0.79, 0.86, 0.88, ],
'B': [ 0.89, 0.89, 0.90, ],
}
df2 = pd.DataFrame.from_dict(DATAdict, orient='columns')
unique = pd.unique(df1.values.ravel('K'))
markers = {}
marker_list = ["s", "o", "+", "x", "P"]
for label, i in zip(unique, range(0, len(unique))):
markers[label] = marker_list[i]
想法是绘制 A 列,使用 range(0,3)
作为 x 轴,使用 0.79、0.86 等作为 y 轴,然后 select 基于 foo、miau 和 ciao 的标记。
最简单的方法是使用 sns.scatterplot
和 style
参数:
style
: vector or key in dataGrouping variable that will produce points with different markers. Can have a numeric dtype but will always be treated as categorical.
要连接线,我不知道有什么方法可以用 sns.scatterplot
做到这一点,但我们可以使用 Axes 句柄覆盖线:
import seaborn as sns
ax = sns.scatterplot(data=df2.reset_index(), x='index', y='A', style=df1['A'], s=100)
ax.plot(df2.index, df2['A'])
请注意,seaborn 会自动选择 style
标记。您可以使用 markers
参数手动设置它们, 但是 标记不能混合 filled and unfilled markers:
marker_list = ['s', 'o', 'P', 'X'] # all "filled" type
ax = sns.scatterplot(data=df2.reset_index(), x='index', y='A', style=df1['A'], markers=marker_list, s=100)
我在 matplotlib 中找不到不单独绘制散点标记的方法:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
DATAdict = {
'A': [ "foo", "miau", "ciao" ],
'B': [ "miau", "haha", "ciao"],
}
df1 = pd.DataFrame.from_dict(DATAdict, orient='columns')
DATAdict = {
'A': [ 0.79, 0.86, 0.88, ],
'B': [ 0.89, 0.89, 0.90, ],
}
df2 = pd.DataFrame.from_dict(DATAdict, orient='columns')
marker_list = ["s", "o", "+", "x", "P"]
marker_dict = dict(zip(pd.unique(df1.values.flat), marker_list))
ax = df2.plot()
row, col = df1.shape
for x, y, m, c in zip(np.repeat(np.arange(row), col), df2.values.flat, df1.values.flat, np.tile(np.arange(col), row)):
if not c:
ax.set_prop_cycle(None)
ax.scatter(x, y, marker=marker_dict[m])
plt.show()
示例输出: