在同一个图形 seaborn 上绘制多列 [英] plot multiple columns on same graph seaborn

查看:43
本文介绍了在同一个图形 seaborn 上绘制多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 seaborn 绘制的 Pandas 数据框:

I have a pandas dataFrame that I am plotting with seaborn:

g = sns.FacetGrid(readCov, col='chr', col_wrap = 4, size=4)
g.map(plt.scatter, 'pos', 'bergC9', hue =  edgecolor='white')
g.set(xlim= (0, 250000))

这很好用,并为chr"列中的每个chr"提供了一个图表.但是,我希望每个图表上都有多个列.目前只显示一个,称为bergC9".我想在同一个图表上用不同的颜色放置更多的列.

This works great and gives me a single graph for each 'chr' that is in the 'chr' column. However, I would like each graph to have multiple columns on it. Currently only one is displayed, the one called 'bergC9'. I want to put more columns on the same graph with different colors.

有什么想法吗?

谢谢!

输入数据文件

chr description pos bergB7  bergC9  EvolB20
1   1   '"ID=PBANKA_010290;Name=PBANKA_010290;descript...   108389  0.785456    0.899275    0.803017
2   1   '"ID=PBANKA_010300;Name=PBANKA_010300;descript...   117894  1.070673    0.964203    0.989372
3   1   '"ID=PBANKA_010310;Name=PBANKA_010310;descript...   119281  1.031106    1.042189    0.883518
4   1   '"ID=PBANKA_010320;Name=PBANKA_010320;descript...   122082  0.880109    1.031673    1.026539
5   1   '"ID=PBANKA_010330;Name=PBANKA_010330;descript...   126075  0.948105    0.969198    0.849213

我想要一个散点图,它以 pos 作为 x 轴和 bergB7、bergC9、EvolB20 等,它们都是应变"作为 y 轴,因此在同一张图上有几个应变.我能够通过重新格式化我的数据集来实现这一点,因此它现在有一个应变"参数或列,并连接了所有 y 数据.现在我可以使用带有应变"的色调语法.我不想重新格式化我的所有数据集.我认为可以创建一个循环来引用我想要绘制的所有列,但我尝试了几种语法都无济于事.我还想过其他方法来实现这一点,但这些方法会创建新的数据集,我知道这不是以编程方式进行的方法.我是新用户,希望正确开始.

I would like a scatterplot that has pos as the x-axis and bergB7, bergC9, EvolB20 etc, which are all 'strains' as the y-axis, thus several strains on the same graph. I was able to accomplish this by reformatting my data set so it now has a 'strain' parameter or column and concatenated all of the y data. Now I can use the hue syntax with 'strain'. I would like to not have to reformat all of my data sets. I thought that it may be possible to create a loop that would reference all the columns I want plotted, but I tried several syntaxes to no avail. There are other ways I've thought of to accomplish this, but these create new datasets and I know is not the way to go programmatically. I am a new user and would like to start out correctly.

这是输出的样子(显示了 15 个图形面板的子集):(因为我的'声誉'不够高,我无法发布图片)

This is what the output should look like (subset of 15 graph panel shown): (I cannot post the image because my 'reputation' is not high enough)

推荐答案

编辑数据有两个 chr 案例.应该适用于任意数量的菌株"列.数据确实需要重新格式化;来自 seaborn 文档:

Edited the data to have two chr cases. Should work for any number of "strains" columns. The data does need reformatting; from the seaborn documentation:

要使用这些功能,您的数据必须位于 Pandas DataFrame 中,并且它必须采用 Hadley Whickam 所说的整洁"数据的形式.在简而言之,这意味着您的数据框的结构应该使每个列是一个变量,每一行是一个观察值.

To use these features, your data has to be in a Pandas DataFrame and it must take the form of what Hadley Whickam calls "tidy" data. In brief, that means your dataframe should be structured such that each column is a variable and each row is an observation.

但熊猫很容易做到:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
readCov = pd.DataFrame([ (1, '\'"ID=PBANKA_010290;Name=PBANKA_010290;descript...', 108389, 0.785456, 0.899275, 0.803017),
       (1, '\'"ID=PBANKA_010300;Name=PBANKA_010300;descript...', 117894, 1.070673, 0.964203, 0.9893719999999999),
       (1, '\'"ID=PBANKA_010310;Name=PBANKA_010310;descript...', 119281, 1.0311059999999999, 1.042189, 0.883518),
       (2, '\'"ID=PBANKA_010320;Name=PBANKA_010320;descript...', 122082, 0.880109, 1.031673, 1.0265389999999999),
       (2, '\'"ID=PBANKA_010330;Name=PBANKA_010330;descript...', 126075, 0.948105, 0.969198, 0.8492129999999999)],
       columns=[u'chr', u'description', u'pos', u'bergB7', u'bergC9', u'EvolB20'],
       )

meltCov = pd.melt(readCov,id_vars=['chr','description','pos'], var_name='strain')
g = sns.FacetGrid(meltCov, col='chr', hue='strain')
g.map(plt.scatter, 'pos','value')
g.set_xticklabels(rotation=45)
g.add_legend()

#this plots a figure per script automatically
from os.path import realpath, basename 
s = basename(realpath(__file__))
fig = plt.gcf()
fig.savefig(s.split('.')[0])
plt.show()

这篇关于在同一个图形 seaborn 上绘制多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆