pandas 在同一个图形 seaborn 上绘制多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29712334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
plot multiple columns on same graph seaborn
提问by pinkvirus
I have a pandas dataFrame that I am plotting with seaborn:
我有一个我正在用 seaborn 绘制的 Pandas 数据框:
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))
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”列中的每个“chr”提供了一个图表。但是,我希望每个图表上都有多个列。目前只显示一个,一个叫做'bergC9'。我想在同一个图表上用不同的颜色放置更多的列。
Any ideas?
有任何想法吗?
Thanks!
谢谢!
edit: input data file
编辑:输入数据文件
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
EDIT: 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.
编辑:我想要一个散点图,它以 pos 作为 x 轴和 bergB7、bergC9、EvolB20 等,它们都是“应变”作为 y 轴,因此在同一张图上有几个应变。我能够通过重新格式化我的数据集来实现这一点,因此它现在有一个“应变”参数或列,并连接了所有 y 数据。现在我可以使用带有“应变”的色调语法。我不想重新格式化我的所有数据集。我认为可以创建一个循环来引用我想要绘制的所有列,但我尝试了几种语法都无济于事。我还想过其他方法来实现这一点,但这些方法会创建新的数据集,我知道这不是以编程方式进行的方法。我是一个新用户,想正确开始。
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)
这就是输出的样子(显示了 15 个图形面板的子集):(我无法发布图像,因为我的“声誉”不够高)
回答by cphlewis
Edited the data to have two chrcases. Should work for any number of "strains" columns. The data does need reformatting; from the seaborn documentation:
编辑数据有两种chr情况。应该适用于任意数量的“菌株”列。数据确实需要重新格式化;来自seaborn文档:
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.
要使用这些功能,您的数据必须位于 Pandas DataFrame 中,并且必须采用 Hadley Whickam 所说的“整洁”数据的形式。简而言之,这意味着您的数据框的结构应该使每一列都是一个变量,每一行都是一个观察值。
but pandas does it easily:
但Pandas很容易做到:
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()



