我有以下条形图,其中标准偏差太小而看不到。 y轴使用对数刻度。是否可以配置脚本以显示标准偏差?
from os import listdir
from os.path import isfile, join
import csv
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from collections import namedtuple
from matplotlib.pyplot import figure
NS_FILE = '..\\overhead.csv'
OUTPUT_PATH = '..\\'
sboti = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[1], names=['sboti'])
sboti_nds = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[2], names=['sboti_nds'])
greedy = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[3], names=['greedy'])
simdijkstra = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[4], names=['simdijkstra'])
random = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[5], names=['random'])
sboti_stdev = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[7], names=['sboti_stdev'])
sboti_nds_stdev = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[8], names=['sboti_nds_stdev'])
greedy_stdev = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[9], names=['greedy_stdev'])
simdijkstra_stdev = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[10], names=['simdijkstra_stdev'])
random_stdev = pd.read_csv(filepath_or_buffer=NS_FILE, header=None, usecols=[11], names=['random_stdev'])
sboti_list = sboti['sboti'].values.tolist()
sboti_nds_list = sboti_nds['sboti_nds'].values.tolist()
greedy_list = greedy['greedy'].values.tolist()
simdijkstra_list = simdijkstra['simdijkstra'].values.tolist()
random_list = random['random'].values.tolist()
sboti_stdev_list = sboti_stdev['sboti_stdev'].values.tolist()
sboti_nds_stdev_list = sboti_nds_stdev['sboti_nds_stdev'].values.tolist()
greedy_stdev_list = greedy_stdev['greedy_stdev'].values.tolist()
simdijkstra_stdev_list = simdijkstra_stdev['simdijkstra_stdev'].values.tolist()
random_stdev_list = random_stdev['random_stdev'].values.tolist()
n_groups = 4
fig, ax = plt.subplots()
fig.set_size_inches(10.5, 4.5, forward=True)
plt.yticks(fontsize=24)
index = np.arange(n_groups)
bar_width = 0.55
error_config = {'ecolor': '0.3'}
ax.grid(which='major', axis='y', linestyle='--')
ax.bar(index, sboti_list, bar_width/5, color='r',
yerr=sboti_stdev_list, error_kw=error_config, label='Sboti')
ax.bar(index + bar_width * 0.25, sboti_nds_list, bar_width/5, color='cornflowerblue',
yerr=sboti_nds_stdev_list, error_kw=error_config, label='Sboti_nds')
ax.bar(index + bar_width * 0.5, greedy_list, bar_width/5, color='g',
yerr=greedy_stdev_list, error_kw=error_config, label='GoCoMo')
ax.bar(index + bar_width * 0.75, simdijkstra_list, bar_width/5, color='yellow',
yerr=simdijkstra_stdev_list, error_kw=error_config, label='SimDijkstra')
ax.bar(index + bar_width * 1.0, random_list, bar_width/5, color='orange',
yerr=random_stdev_list, error_kw=error_config, label='Random')
ax.set_xlabel('Configuration Size', fontsize=24, fontweight="bold")
ax.set_ylabel('Communication \n Overhead (#)', fontsize=24, fontweight="bold")
ax.set_xticks(index + bar_width * 0.6)
ax.set_xticklabels(('k=2', 'k=3', 'k=4', 'k=5'), fontsize=26)
ax.legend()
plt.legend(['SBOTI', 'SBOTI-NDS', 'GoCoMo', 'SimDijkstra', 'Random'],
fontsize=24, loc='upper center', ncol=1, bbox_to_anchor=(1.225, 1.05),
borderpad=0.5, edgecolor='black', fancybox=False, handletextpad=0.05)
ax.set_yscale('log',nonposy='clip')
plt.ylim([0.0, 1000000.0])
fig.tight_layout()
plt.savefig(join(OUTPUT_PATH, 'overhead_plot_final.pdf'), bbox_inches='tight')
plt.show()
header.csv的内容:
k2,37460.22,37460.42,5928.02,60.56,352.26,,21.92593283334245,25.202923380805963,3128.397082487178,20.88858686518764,242.4525040800649
k3,99930.06,99932.76,4358.62,25.2,217.42,,76.54621386549994,82.02651636973927,516.8570595435453,5.941895526595833,26.101911729188863
k4,212409.94,212406.28,9603.56,29.5,430.18,,166.80495881632027,153.9937767096649,7.52725659432005,9.653697610295259,17.54724235558722
k5,416936.9,416930.14,18754.28,35.04,844.4,,31.603571226773,29.936674661623975,4.549231825713175,10.540417836275054,31.93040902350239
答案 0 :(得分:0)
您可以替换如下代码:
sboti_list = sboti['sboti'].values.tolist()
sboti_stdev_list = sboti_stdev['sboti_stdev'].values.tolist()
ax.bar(index, sboti_list, bar_width/5, color='r',
yerr=sboti_stdev_list, error_kw=error_config, label='Sboti')
通过删除将数据框列转换为列表并使用的前两行
ax.bar(index, sboti['sboti'], bar_width/5, color='r',
yerr=sboti_stdev['sboti_stdev'], error_kw=error_config, label='Sboti')
相反。这样可以清理一些代码。