如何在Python中制作一个分散的条形图

huangapple go评论76阅读模式
英文:

How do you make a diverging barchart in python

问题

我正在尝试用Python制作一个分散的条形图,用于显示致病突变(表示为-1)和非致病突变(表示为1)。每个突变出现在基因的特定位置(1-250),并在每个位置发生一定次数(例如,您可以在位置24处有8次发生,在位置35处有16次发生等)。

我希望条形图中显示致病突变向下,非致病突变向上,其中x轴表示位置(1-250),y轴表示每个位置上的突变数量,因此条形的高度对应于该位置上的突变数量。

有人有什么想法吗?

我希望它看起来有点像这样:
如何在Python中制作一个分散的条形图

虚拟数据:

import random
from random import choice
import pandas as pd

position = random.randint(1, 250)
pathogenicity = choice([-1, 1])

df = pd.DataFrame({'Position': [position], 'Pathogenicity': [pathogenicity]})
英文:

I'm trying to make a diverging barchart in python to show pathogenic mutations (represented as -1) and non-pathogenic mutations (represented as 1). Each appears at a certain position on the gene (1-250) and occurs a certain number of times per location (so you could have 8 occurring at position 24, 16 at 35 etc.).

I want the barchart to show pathogenic mutations going down and non-pathogenic going up, with x being position (1-250) and y being the count of each mutation at the given position, ergo the height of the bars corresponds to how many are at the position.

Does anyone have any ideas?

I want it to look a bit like this:
如何在Python中制作一个分散的条形图

Dummy data:

import random
from random import choice
import pandas as pd

position = random.randint(1, 250)
pathogencity = choice([i for i in range(-1,1) if i not in 
[0]]))

df = pd.concat([pathogencity,position],axis=1) 

答案1

得分: 3

以下是代码部分的中文翻译:

导入:

import matplotlib.pyplot as plt
import numpy as np

虚拟数据:

N = 18  # 虚拟数据和图表的大小,我根据您的图表选择了这个大小
positions = np.arange(1, N)
counts = np.random.randint(1, 20, size=N-1)
mut_types = np.random.choice([-1, 1], size=N-1)
# 根据突变类型拆分数据
pos_counts = counts * (mut_types == 1)
neg_counts = counts * (mut_types == -1)

方法:

# 生成柱状图
fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(positions, pos_counts, color='green')
ax.bar(positions, -neg_counts, color='red')
# 设置y轴限制
max_y = max(abs(pos_counts).max(), abs(neg_counts).max())
ax.set_ylim(-max_y, max_y)
# 设置x轴限制和标签
ax.set_xlim(0, N)
ax.set_xticks(np.arange(0, N, 25))
ax.set_xlabel('位置')
ax.set_ylabel('计数')
ax.set_title('位置和类型的突变计数')

plt.show()

结果:

如何在Python中制作一个分散的条形图

附加内容:

  1. 如果您想要添加网格线,请使用 ax.grid(visible=True, axis='both', linestyle='--', alpha=0.7, color='black', linewidth=1.5)

  2. 如果您只想在顶部中间和底部标记为"Negative"、"Natural"和"Positive",您可以使用以下方法: y_ticks = ax.get_yticks() 然后 plt.yticks([min(y_ticks), 0, max(y_ticks)], ["Negative", "Natural", "Positive"])

英文:

You can accomplish it like so:

Imports:

import matplotlib.pyplot as plt
import numpy as np

Dummy data:

N = 18 # Size of dummy data and graph, I took it based on your graph
positions = np.arange(1, N)
counts = np.random.randint(1, 20, size=N-1)
mut_types = np.random.choice([-1, 1], size=N-1)
# Splitting the data by mutation type
pos_counts = counts * (mut_types == 1)
neg_counts = counts * (mut_types == -1)

Method:

# Generating the bar chart
fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(positions, pos_counts, color='green')
ax.bar(positions, -neg_counts, color='red')
# Setting the y-axis limits
max_y = max(abs(pos_counts).max(), abs(neg_counts).max())
ax.set_ylim(-max_y, max_y)
# Setting the x-axis limits and labels
ax.set_xlim(0, N)
ax.set_xticks(np.arange(0, N, 25))
ax.set_xlabel('Position')
ax.set_ylabel('Count')
ax.set_title('Mutation Counts by Position and Type')

plt.show()

Result:

如何在Python中制作一个分散的条形图

Additional:

  1. If you want to add a gridline, use ax.grid(visible= True, axis='both', linestyle='--', alpha=0.7, color='black', linewidth=1.5)

  2. If you want to label only as Positive-Natural-Negative, in the top mid and bottom, you can use this y_ticks = ax.get_yticks() and then plt.yticks([min(y_ticks), 0, max(y_ticks)], ["Negative", "Natural", "Positive"])

答案2

得分: 2

以下是您要翻译的内容:

I suppose you could do:

import matplotlib.pyplot as plt

# define data here
data = [-1, 2, 1, 5, -3]
x = range(1, len(data) + 1)

colors = ["green" if i > 0 else "red" for i in data]
plt.yticks([-2, 0, 2], ["negative", "neutral", "positive"])

plt.xlabel("Respondent")
plt.ylabel("Overall response sentiment")
plt.bar(x, data, color=colors)

plt.show()

Output:
如何在Python中制作一个分散的条形图

Edit:
You can get the dashed x=0 line by adding:

plt.plot([0, x[-1] + 1], [0, 0], color="black", ls="--")

to your code.

New output:
如何在Python中制作一个分散的条形图

英文:

I suppose you could do:

import matplotlib.pyplot as plt

# define data here
data = [-1, 2, 1, 5, -3]
x = range(1, len(data) + 1)

colors = ["green" if i > 0 else "red" for i in data]
plt.yticks([-2, 0, 2], ["negative", "neutral", "positive"])

plt.xlabel("Respondent")
plt.ylabel("Overall response sentiment")
plt.bar(x, data, color=colors)

plt.show()

Output:
如何在Python中制作一个分散的条形图

Edit:
You can get the dashed x=0 line by adding:

plt.plot([0, x[-1] + 1], [0, 0], color="black", ls="--")

to your code.

New output:
如何在Python中制作一个分散的条形图

huangapple
  • 本文由 发表于 2023年2月26日 21:17:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定