我正在使用matplotlib创建一个用于分类变量的图,并且我需要为每个柱形(categorie
)放置观察数。有谁能够帮助我?我只找到手动放置的解决方案,但是我需要为几个变量做相同的图,因此我需要一些代码来对变量上每个类别的观测次数进行计数。谢谢你!
这是我的代码:
axis_x = 'Action intended to block/propose policy (block = 1)'
axis_y = 'Take up'
title = 'neighborhood attract more people'
type = 'bar'
column1 = 'g_scope'
column2 = 'take_up'
database = df
graph_mean_diff_c(column1, column2, tipo, savename, titulo, eixo_x, eixo_y, database)
答案 0 :(得分:0)
类似这样的东西:
这里是代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import matplotlib.ticker as ticker
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(x)[0:5], 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(x)[0:5], xy=(x-0.01, y), va='center',ha='right', color='r', fontweight='bold', fontsize=16)
x_majorLoc = MultipleLocator(0.5); ax1.xaxis.set_major_locator(x_majorLoc)
#ax.xaxis.set_major_locator(ticker.IndexLocator(base=0.0, offset=0.1))
plt.show()
pic_name='psc_annotaed_bars.png'
fig.savefig(pic_name, transparency=True)
与竖线不同。我试过了 here