用pyhton画带饼图的柱状图(bar with pie)
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import matplotlib as mpl
mpl.rcParams['font.family'] = 'times new roman'
# 设置字体
# plt.rcParams['font.family'] = ['Times New Roman']
data = {
'Class': ['5UTR', 'TSS', 'CDS', 'TTS', '3UTR'],
'Count': [431, 783, 5801, 595, 2391]
}
df = pd.DataFrame(data)
# 设置颜色
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd']
# 绘制柱状图
fig, ax = plt.subplots(figsize=(10, 6))
bars = ax.bar(df['Class'], df['Count'], color=colors[:len(df)])
ax.set_xlabel('Class', fontsize=12)
ax.set_ylabel('Count', fontsize=12)
# 旋转 x 轴标签
plt.xticks(rotation=25)
# plt.subplots_adjust(bottom=0.183)
# 添加数值标签
for bar in bars:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width() / 2, height, height, ha='center', va='bottom')
# 创建新的 Axes 对象,并绘制饼状图
ax2 = inset_axes(ax, width="40%", height="40%", loc='upper right')
ax2.pie(df['Count'], radius=1, labeldistance=0.6, colors=colors[:len(df)])
# plt.savefig('class.pdf')
# 显示图形
plt.show()
更简单的方法:利用微生信
很好用的网站,推荐推荐