我想将 y 轴设置为始终比最大 y 值高 20 个单位。
I want to set the y axis to always be 20 units higher than the max y value.
因为这是矩阵数据,所以很难说 yvals = [1, 2, 3]。我需要知道如何从我的数据集中 select 最大值,以便 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")
有人,请帮忙,我想这是一个快速修复。填写 space 因为显然,我需要更多内容...
它不是很漂亮,但您可以生成列方向最大值的行方向最大值,反之亦然:
ax.set_ylim(0,df[['x', 'y', 'z', 'a', 'b']].max(axis=1).max(axis=0)+20)
因为这是矩阵数据,所以很难说 yvals = [1, 2, 3]。我需要知道如何从我的数据集中 select 最大值,以便 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")
有人,请帮忙,我想这是一个快速修复。填写 space 因为显然,我需要更多内容...
它不是很漂亮,但您可以生成列方向最大值的行方向最大值,反之亦然:
ax.set_ylim(0,df[['x', 'y', 'z', 'a', 'b']].max(axis=1).max(axis=0)+20)