如何显示来自数据框的堆叠式Barh图表的值?如何将标签放置在每个栏上其相应区域的上方,并修改字体,使其以灰度图形的形式很好地显示?
它与此问题有关,但是它具有一个值列表,而不是从熊猫数据框中提取的两个列表。如果这是一个单列表,我想我可以从数据框中的单个记录中提取值,但可以有两个列表,我不确定如何将其应用于条形图中的每个条形。
我的数据框:
Delin. Group1 Group2 Group3 Group4 Group5 Census 0.2829 0.3387 0.2636 0.0795 0.0353 USPS 0.2538 0.3143 0.2901 0.1052 0.0366
我的代码:
import os import pandas as pd import time # start_time = time.time() # output_dir = r"C:\Some\Directory\For\Ouputs" # output_fig = "race_barh2.png" # fig_path = os.path.join(output_dir, output_fig) # os.chdir(output_dir) # input_csv = r"C:\Some\Directory\To\My.csv" # df = pd.read_csv(input_csv, delimiter = ",") # ax = df.plot.barh( stacked = True, color = ("#252525", "#636363", "#969696", "#cccccc", "#f7f7f7"), edgecolor = "black", linewidth = 1) # ax.set_xlabel("Percentage of Total", fontsize = 18) # ax.set_ylabel("Boundary Delineation", fontsize = 18) # ax.set_yticklabels(["Census", "USPS"]) # ax.set_xticklabels(["0%", "20%", "40%", "60%", "80%", "100%"]) # horiz_offset = 1.03 # vert_offset = 1 # ax.legend(bbox_to_anchor=(horiz_offset, vert_offset)) # fig = ax.get_figure() # fig.savefig(fig_path, bbox_inches = "tight", dpi = 600) # # # end_time = round( time.time() - start_time, 5 ) # print "Seconds elapsed: {0}".format(end_time)
您可以通过注释条与参考问题类似地执行此操作。对于堆积的条形图,您必须稍微调整标签的位置,以使其位于所需位置。您可以使用horizontalalignment,verticalalignment并像我一样添加一点空白(+.5)。
horizontalalignment
verticalalignment
import numpy as np import pandas as pd import matplotlib.pyplot as plt from cycler import cycler #used gray colormap, you can use your own colors by replacing colormap='gray' with color=colors colors = ["#252525", "#636363", "#969696", "#cccccc", "#f7f7f7"] plt.rcParams['axes.prop_cycle'] = cycler(color=colors) #dummy data df = pd.DataFrame(np.random.randint(5, 8, (10, 3)), columns=['Group1', 'Group2', 'Group3']) for col in df.columns.tolist(): df[col] = df[col].apply(lambda x:x*100 / df[col].sum()) ax = df.T.plot.barh(stacked=True, colormap='gray', edgecolor='black', linewidth=1) for lbl in ax.patches: ax.annotate("{:.0f}%".format(int(lbl.get_width())), (lbl.get_x(), lbl.get_y()+.5), verticalalignment='bottom', horizontalalignment='top', fontsize=8, color='black') ax.legend(loc='center left', bbox_to_anchor=(1.0, .5)) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) plt.show()