我有以下数据帧
import pandas as pd
data_tmp = pd.DataFrame({'x': [0,14,28,42,56, 0,14,28,42,56],
'y': [0, 0.003, 0.006, 0.008, 0.001, 0*2, 0.003*2, 0.006*2, 0.008*2, 0.001*2],
'cat': ['A','A','A','A','A','B','B','B','B','B'],
'color': ['#B5D8F0','#B5D8F0','#B5D8F0','#B5D8F0','#B5D8F0','#247AB2','#247AB2','#247AB2','#247AB2','#247AB2'],
'point': [14,14,14,14,14,28,28,28,28,28],
'linestyles':['-','-','-','-','-','--','--','--','--','--']})
我想用不同的color
和{cat
生成一个线条图。但是我想给出具体的color
和linestyles
,因为它们在dataframe
中定义。最后,我想用相同的颜色标记每行上的point
。在
我只试过:
^{pr2}$有什么想法吗?在
我就是这样做的。您需要使用} 将字符串值(例如
cat
列来控制不同的打印参数(颜色、样式、标记大小),然后创建映射对象(这里是dicts)来告诉每个类别使用哪个参数值。颜色很容易。linestyle比较困难,因为Seaborn只提供dashes
作为一个可配置参数,它需要以(segment, gap)
的高级Matplotlib格式给出。函数^{)转换为此格式,但返回值需要小心处理。对于标记大小,不幸的是,
lineplot
不能随类别更改标记大小(即使您可以更改标记样式),因此您需要在顶部使用scatterplot
。最后一位是图例,您可能希望在第二次绘图时禁用它,以避免重复,但问题是第一个图例中没有标记。如果这让您感到困扰,您仍然可以手动编辑图例。总而言之,它可能是这样的:输出:
也许你想直接使用matplotlib,比如
这是我在@jdehesa的帮助下的解决方案
我也把传说放在了情节之外,并对标签做了一些修饰
相关问题 更多 >
编程相关推荐