我正在嘗試繪制df2為常規連續圖,但使用 from 的值df1來選擇標記。
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 軸,然后根據 foo、miau 和 ciao 選擇標記。
uj5u.com熱心網友回復:
最簡單的方法是使用
請注意,seaborn 會style自動選擇標記。您可以使用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)
uj5u.com熱心網友回復:
我在 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(np.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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424603.html
標籤:Python 熊猫 数据框 matplotlib
