我无法通过 plotnine 自定义图例aes()
我有以下代码:
import pandas as pd
from plotnine import *
data1 = {'dilution': [2.000000, 2.477121, 2.954243, 3.431364, 3.908485, 4.385606, 4.862728, 5.339849, 5.816970, 2.000000, 2.477121, 2.954243, 3.431364, 3.908485, 4.385606, 4.862728, 5.339849, 5.816970],
'variable': ["mouse 1", "mouse 1", "mouse 1", "mouse 1", "mouse 1", "mouse 1", "mouse 1", "mouse 1", "mouse 1", "mouse 2", "mouse 2", "mouse 2", "mouse 2", "mouse 2", "mouse 2", "mouse 2", "mouse 2", "mouse 2"],
'value': [547.180708, 495.883622, 439.109089, 277.819313, 115.926188, 42.041189, 15.276367, 11.696537, 2.280014, 269.398164, 233.667531, 215.410352, 169.512070, 102.877518, 36.860550, 13.960504, 4.891481, -3.465304]}
df1 = pd.DataFrame.from_dict(data1)
data2 = {'dilution': [2.0, 2.0, 2.0],
'value': [-7.873768, -3.926121, 4.170833] }
df2 = pd.DataFrame.from_dict(data2)
data3 = {'dilution': [3.90309, 3.90309],
'value': [756.715198, 540.613828],
'variable': ["mouse 1", "mouse 2"]}
df3 = pd.DataFrame.from_dict(data3)
g = (ggplot(df1)
+ geom_line(aes(x='dilution', y='value', color='variable'), data=df1, size=1.0)
+ geom_point(aes(x='dilution', y='value', color='variable'), data=df1, size=1.0)
+ geom_point(aes(x='dilution', y='value'), data=df2, size=3.0)
+ geom_point(aes(x='dilution', y='value', color='variable'), data=df3, size=2.0, shape='s')
+ scale_x_continuous( )
)
print(g)
这会产生以下图表:
如您所见,来自的数据点df2
没有出现在图例中。我希望图例中的一个黑点代表df2
. 如果我更改data2
如下,我可以在图例中显示它:
data2 = {'dilution': [2.0, 2.0, 2.0],
'value': [-7.873768, -3.926121, 4.170833],
'type': ['test', 'test', 'test']}
然后将其映射到美学如下:geom_point(aes(x='dilution', y='value', color='type'), data=df2, size=3.0)
但后来这些点不再是黑色的,我似乎无法再次将这些点改回黑色。添加color='black'
参数不起作用:
有没有更好的解决方案来保留所有df2
黑色数据点,而在图例中只出现一次作为黑点?
其次,有没有办法在图例中添加一个黑色方块来表示来自的所有数据点df3
?