我尝试为数据创建条形图。这个想法是Available Shops
条的数量与Users
条重叠,因此在一个图中基本上是两个条图。我的代码可以工作,但是有问题:
当前,即使我只希望Users
都为其创建值标签。我想使用不同的设置(颜色,放置标签的位置等)重新运行“商店”的值标签功能。
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib import gridspec
import seaborn as sns
years = list(np.arange(1999,2015))
users = list(np.random.randint(1500, 5000, 16))
shops = list(np.random.randint(400, 600, 16))
df = pd.DataFrame({"Year": years,
"Users" : users,
"Available Shops" : shops})
# Graph
SMALL_SIZE = 10
MEDIUM_SIZE = 12
BIGGER_SIZE = 14
sns.set_style(style ="whitegrid")
# overall settings:
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=MEDIUM_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
fig = plt.figure(figsize=(7,4))
ax = plt.subplot()
ax1 = sns.barplot(x="Year", y="Users", data=df, color='blue')
ax2 = sns.barplot(x="Year", y='Available Shops', data=df, color='red')
ax.set_xlabel("Year")
ax.set_ylabel("Number")
for p in ax1.patches:
ax1.text(p.get_x() + p.get_width()/2., p.get_height() + 1, '%d' % int(p.get_height()),
fontsize=SMALL_SIZE, color='black', ha='center', va='bottom')
ax.grid(False)
sns.despine(top=True, right=True)
plt.tight_layout()
plt.show()
如有必要,我可以模拟一个数据框。