简体   繁体   English

使用Python在子图中绘制Seaborn Barplots

[英]Plot Seaborn Barplots in Subplots with Python

I have two files as input, that look as follows: 我有两个文件作为输入,如下所示:

col1 col2
A B
C C
B A
A A
A C
C A
B B

Meaning, I have two columns with letters, separated by spaces. 意思是,我有两列用字母隔开的空格。 I want to plot the number of the occurrences of those letters, each column in its own barplot. 我想绘制这些字母的出现次数,每一列都在其自己的barplot中。 Assume that both files have a different distribution of letters. 假设两个文件的字母分布不同。

This is the code: 这是代码:

from collections import Counter
from os.path import isfile, join
from os import listdir
import matplotlib.pyplot as plt

import seaborn as sns
sns.set(color_codes=True)

inputDir = "/tmp/files/"

inputFiles = [ f for f in listdir(inputDir) if isfile(join(inputDir, f)) ]

fig, axes = plt.subplots(figsize=(6,6), ncols=2, nrows=len(inputFiles))

z=0

while inputFiles:

  files = inputFiles[0]
  inputFiles.remove(files)

  c = Counter()
  a = Counter()

  x1 = []
  y1 = []
  x2 = []
  y2 = []

  with open(inputDir + files, "r") as f2:
    for line in f2:
      line = line.strip()
      if line.split(" ")[0] != "col1":
        c[str(line.split(" ")[0])] += 1
        a[str(line.split(" ")[1])] += 1

  try:
    for cc in c:
      x1.append(cc)
      y1.append(c[cc])
    row = z // 2
    col = z % 2
    ax_curr = axes[row, col]
    sns.barplot(x1, y1, ax=ax_curr)

    z+=1

    for aa in a:
      x2.append(aa)
      y2.append(a[aa])
    row = z // 2
    col = z % 2
    ax_curr = axes[row, col]
    sns.barplot(x2, y2, ax=ax_curr)

    z+=1

  except:
    continue

sns.plt.show()

The result should be one image, where I have the following barplots as subplots: 结果应该是一张图像,其中有以下条形图作为子图:

---------------------------------------
|                  |                  |
|                  |                  |
|   barplot col1   |   barplot col2   |
|        file1     |       file1      |
|                  |                  |
--------------------------------------|
|                  |                  |
|                  |                  |
|   barplot col1   |   barplot col2   |
|        file2     |       file2      |
|                  |                  |
---------------------------------------

So the height of each bar should correspond to the number of each letter. 因此,每个条的高度应对应于每个字母的数量。

The problem until now is, that the bars in each subplot look completely different and I cant find out why. 到目前为止的问题是,每个子图中的条形看起来完全不同,我无法找到原因。 Please let me know, if I can provide more information. 如果可以提供更多信息,请告诉我。

Although it is unclear what "completely" different means here, it could be that you need to strip the lines before splitting them. 尽管目前尚不清楚“完全”不同意味着什么,但可能是您需要在分割线之前先去除线。 Otherwise the values of the last column could look like "B " instead of "B" . 否则,最后一列的值可能看起来像"B "而不是"B" Also I'm not sure why you try to convert a string to an integer in c[int(line.split(" ")[0])] += 1 . 另外,我不确定您为什么尝试在c[int(line.split(" ")[0])] += 1字符串转换为整数。 That doesn't make much sense to me. 这对我来说没有多大意义。

Try: 尝试:

with open(inputDir + files, "r") as f2:
      for line in f2:
          line = line.strip()
          if line.split(" ")[0] != "col1":
              c[line.split(" ")[0]] += 1
              a[line.split(" ")[1]] += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM