由于这是矩阵数据,很难说yvals = [1,2,3]。我需要知道如何从我的数据集中选择最大值,以便ax.set_ylim(0,max(val)+20)或类似的东西。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#Sets the size of the chart
from pylab import rcParams
rcParams['figure.figsize'] = 7, 2
#Creates the dataframe
raw_data = {'plan_type': ['Total Annual Price', 'Total Annual Cost,
Family'],
'x': [60, 30],
'y': [48, 16],
'z': [18, 28],
'a': [49, 13],
'b': [57, 16],
}
df = pd.DataFrame(raw_data,
columns = ['plan_type','x', 'y', 'z', 'a', 'b'])
#Plots the bars, adding desired colors
ax = df.plot.bar(rot=0, color=['#ffc000',"#305496", '#8ea9db', '#b4c6e7',
'#D9E1F2'],
width = 0.8 )
# Title String
plt.title("Average Annual Medical Cost Insurance per Employee",y=1.08)
# Subtitle string and postitioning
subtitle_string=("**xyz")
plt.suptitle(subtitle_string,fontsize=8, y=0.8)
#Adds data labels to top of bars
for p in ax.patches[0:]:
h = p.get_height()
x = p.get_x()+p.get_width()/2.
if h != 0:
ax.annotate("%g" % p.get_height()+'%', xy=(x,h), xytext=(0,4),
rotation=0,
textcoords="offset points", ha="center", va="bottom")
#Sets x-axis limits and margins
ax.set_xlim(-0.5, +1.5)
ax.margins(y=0)
# Set the Y Axis Limits
ax.set_ylim(0,(yvals+20))
ax.margins(y=0)
#Adds legend to the top of the page
ax.legend(ncol=len(df.columns), loc="Lower Left", bbox_to_anchor=
(0,1.02,1,0.06),
borderaxespad=0, mode="expand")
#Add labels to the x-axis
ax.set_xticklabels(df["plan_type"])
#shows the plot and prints it to
plt.show()
plt.savefig("PlanOffered.png")
有人,请帮忙,我想象一下快速修复。填补空间,因为显然,我需要更多的内容......
答案 0 :(得分:1)
它并不漂亮,但你可以生成逐列最大行的最大值,反之亦然:
ax.set_ylim(0,df[['x', 'y', 'z', 'a', 'b']].max(axis=1).max(axis=0)+20)