简体   繁体   中英

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. 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" . Also I'm not sure why you try to convert a string to an integer in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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