如何将颜色和形状融合到人造皮的传说中?在R中似乎有可能。但是我无法在Plotine中使用它。
这是一个例子:
from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap
from plotnine.data import mtcars
(
ggplot(mtcars, aes('cyl', 'mpg', color='factor(gear)', shape='factor(vs)'))
+ geom_jitter()
)
这将创建以下图形:
我想在传奇中加入装备和vs。因此,红色圆圈表示齿轮= 3,vs = 0;红色三角形表示齿轮= 3,vs = 1;等等
...就像以下有关R的帖子中的内容一样:
这可能在紫胶中吗?任何帮助是极大的赞赏!
这是第二个链接的答案的python改编版
如果要更改图例名称,则必须name
在两个scale_*_manual
函数中都使用参数。
from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap,geom_jitter
from plotnine.data import mtcars
import plotnine as p9
# add a column that combines the two columns
new_mtcars = mtcars
new_mtcars['legend_col'] = ['Gear: {} Vs: {}'.format(gear,vs)
for gear,vs in zip(new_mtcars.gear,mtcars.vs)]
# specify dicts to use for determining colors and shapes
gear_colors = {3:'red',4:'blue',5:'gray'}
vs_shapes = {0:'^',1:'o'}
# make the plot with scale_*_manual based on the gear and vs values
(
ggplot(mtcars, aes('cyl', 'mpg', color='legend_col', shape='legend_col'))
+ geom_jitter()
+ p9.scale_color_manual(values=[[gear_colors[i] for i in list(new_mtcars.gear.unique())
if 'Gear: {}'.format(i) in label][0]
for label in new_mtcars.legend_col.unique()],
labels = list(new_mtcars.legend_col.unique()),
name='My legend name')
+ p9.scale_shape_manual(values=[[vs_shapes[i] for i in list(new_mtcars.vs.unique())
if 'Vs: {}'.format(i) in label][0]
for label in new_mtcars.legend_col.unique()],
labels = list(new_mtcars.legend_col.unique()),
name='My legend name')
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句