【发布时间】:2021-08-12 15:23:15
【问题描述】:
我有多个列需要一次绘制。在 for 循环中,我无法为每一列定义 y 刻度,因为它们具有不同的值范围。我想要一个代码,如图所示为每列的情节进行轴设置
这是我正在使用的代码的 sn-p
import random
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import openpyxl
import os
from PIL import Image
import io
x = np.random.randint( 10, 100, size = (50, 4) )
y = np.random.randint( 1, 20, size = (50, 4) )
z = np.concatenate((x,y), axis = 1)
df = pd.DataFrame( z, columns=list('ABCDEFGH') )
columns=list('ABCDEFGH')
cat = ['Cat1','Cat2','Cat3']
cat_list = random.choices(cat,k = 50)
df["Cat"] = cat_list
barWidth = 0.25
# loop over multiple colums to plot as bar graph
for i in columns[0:6]:
print(i)
# The bars classidied according to
bars0 = [ df[df['Cat'] == 'Cat1' ][i].mean(),
df[df['Cat'] == 'Cat1' ][i].mean(),
df[ df['Cat'] == 'Cat1' ][i].mean(),
df[ df['Cat'] == 'Cat1' ][i].mean(),
df[ df['Cat'] == 'Cat1' ][i].mean(),
df[ df['Cat'] == 'Cat1' ][i].mean() ]
# bars 0 represents all the Cat 1 participants
yerr0 = [
df[df['Cat'] == 'Cat1' ][i].std(),
df[df['Cat'] == 'Cat1' ][i].std(),
df[ df['Cat'] == 'Cat1' ][i].std(),
df[ df['Cat'] == 'Cat1' ][i].std(),
df[ df['Cat'] == 'Cat1' ][i].std(),
df[ df['Cat'] == 'Cat1' ][i].std() ]
bars1 = [ df[df['Cat'] == 'Cat2' ][i].mean(),
df[df['Cat'] == 'Cat2' ][i].mean(),
df[ df['Cat'] == 'Cat2' ][i].mean(),
df[ df['Cat'] == 'Cat2' ][i].mean(),
df[ df['Cat'] == 'Cat2' ][i].mean(),
df[ df['Cat'] == 'Cat2' ][i].mean() ]
# bars 1 represents all the Cat 2 participants
yerr1 = [
df[df['Cat'] == 'Cat2' ][i].std(),
df[df['Cat'] == 'Cat2' ][i].std(),
df[ df['Cat'] == 'Cat2' ][i].std(),
df[ df['Cat'] == 'Cat2' ][i].std(),
df[ df['Cat'] == 'Cat2' ][i].std(),
df[ df['Cat'] == 'Cat2' ][i].std() ]
bars8 = [ df[df['Cat'] == 'Cat3' ][i].mean(),
df[df['Cat'] == 'Cat3' ][i].mean(),
df[ df['Cat'] == 'Cat3' ][i].mean(),
df[ df['Cat'] == 'Cat3' ][i].mean(),
df[ df['Cat'] == 'Cat3' ][i].mean(),
df[ df['Cat'] == 'Cat1' ][i].mean()]
# bars 8 represents all the Cat 3 participants
yerr8 = [
df[df['Cat'] == 'Cat3' ][i].std(),
df[df['Cat'] == 'Cat3' ][i].std(),
df[ df['Cat'] == 'Cat3' ][i].std(),
df[ df['Cat'] == 'Cat3' ][i].std(),
df[ df['Cat'] == 'Cat3' ][i].std(),
df[ df['Cat'] == 'Cat3' ][i].std() ]
# standard deciation for the y error bar
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
fig, ax = plt.subplots()
right_side = ax.spines["right"]
right_side.set_visible(False)
top_side = ax.spines["top"]
top_side.set_visible(False)
# Cat1
plt.bar(r1 ,bars0,color='r', width=barWidth, edgecolor='white',
label='Cat1',yerr=yerr0)
# Cat2
plt.bar(r2,bars1,color='g', width=barWidth, edgecolor='white',
label='Cat2', yerr=yerr1)
# Cat3
plt.bar(r3,bars8, color='b', width=barWidth,
edgecolor='white', label='Cat3', yerr=yerr8)
plt.xlabel('columns', fontdict={'fontname': 'Arial', 'fontsize': 16,'fontweight':'bold'})
plt.ylabel('AVG '+ columns[columns.index(i)] ,fontdict={'fontname': 'Arial', 'fontsize': 16})
plt.title(columns[columns.index(i)] ,fontdict={'fontname': 'Arial', 'fontsize': 24})
plt.xticks([r + barWidth for r in range(len(bars1))],
[ 'A', 'B',
'C', 'D', 'E', 'F'],fontsize=12)
当我单独定义 plt.yticks() 时,我能够实现右侧的图表。我无法弄清楚如何循环执行此操作
【问题讨论】:
-
从像
ax = plt.bar(...这样的绘图命令返回轴,然后你可以随意操作子图,例如ax.yticks(... -
@mcsoini 这在单独绘制时有效,在绘制 50 个图的循环中,很难单独操作每个图。如果您可以根据我的代码详细说明您的评论,那将非常有帮助。
标签: python pandas matplotlib plot bar-chart