这是垂直条的版本:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
N=11
t = np.linspace(0,0.5*np.pi,N) #---- generate data ----
df = pd.DataFrame({'A':10*np.sin(t),
'B':10*np.cos(t)} )
fig = plt.figure(figsize=(15,8)) #---- set up graphics ----
ax1 = fig.add_subplot(111)
width=0.9 #---- tuning parameters for the bar plot ----
dy = 0.2
df['A'].plot(kind='bar',width=width, color='b', alpha=0.3) #---- define the bar plot ----
for x,y in enumerate(df['A']): #---- annotate the bars ----
plt.annotate(str(np.around(y,decimals=3)), xy=(x+width/2, y+dy), va='center',ha='right', color='b', fontweight='bold', fontsize=16)
df['B'].plot(kind='bar',width=width, color='r', alpha=0.2) #---- define the bar plot ----
for x,y in enumerate(df['B']): #---- annotate the bars ----
plt.annotate(str(np.around(y,decimals=3)), xy=(x+width/2, y+dy), va='center',ha='right', color='r', fontweight='bold', fontsize=16)
plt.show()
pic_name='psc_annotaed_vertical_bars.png'
fig.savefig(pic_name, transparency=True)
这是单杠的版本:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
N=11
t = np.linspace(0,0.5*np.pi,N)
df = pd.DataFrame({'A':10*np.sin(t),'B':10*np.cos(t)} )
fig = plt.figure(figsize=(15,8))
ax1 = fig.add_subplot(111)
df['A'].plot(kind='barh',width=0.9, color='b', alpha=0.3)
for y, x in enumerate(df['A']):
plt.annotate(str(np.around(x,decimals=3)), xy=(x-0.01, y), va='center',ha='right', color='b', fontweight='bold', fontsize=16)
df['B'].plot(kind='barh',width=0.9, color='r', alpha=0.2)
for y, x in enumerate(df['B']):
plt.annotate(str(np.around(x,decimals=3)), xy=(x-0.01, y), va='center',ha='right', color='r', fontweight='bold', fontsize=16)
plt.show()
pic_name='psc_annotaed_horizontal_bars.png'
fig.savefig(pic_name, transparency=True)