我的代码如下:
def OS_Usage_Zone_Wise(data,zone):
data_os = data['os'].value_counts().plot("bar",figsize=(12,4),fontsize=10)
data_os.set_title("OS usage in "+zone+" region",color='g',fontsize=20)
data_os.set_xlabel("OS name",color='b',fontsize=20)
data_os.set_ylabel("use count",color='b',fontsize=20)
zone = ["east","west","south","central"]
i = 0
data_os = [data_east,data_west,data_south,data_central]
for data in data_os:
OS_Usage_Zone_Wise(data,zone[i])
print("now "+zone[i])
i = i+1
我正在尝试使用for循环绘制每个区域,但是它显示oly graph
对于“中央”区域,我知道它显示最后一个栏而不显示
以前的那些,有没有办法使用循环绘制所有区域的图形(不要
想要一次又一次地使用相同的代码来生成图形)
答案 0 :(得分:3)
您可以使用 matplotlib 创建单独的轴,然后在不同的轴上绘制每个直方图。以下是一些示例代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Create some dummy data
data = pd.DataFrame()
zones = ["east","west","south","central"]
data['zone']=np.hstack([[zone]*np.random.randint(10,20) for zone in zones])
data['OS Usage']=np.random.random(data.shape[0])
# Now create a figure
fig, axes = plt.subplots(1,4, figsize=(12,3))
# Now plot each zone on a particular axis
for i, zone in enumerate(zones):
data.loc[data.zone==zone].hist(column='OS Usage',
bins=np.linspace(0,1,10),
ax=axes[i],
sharey=True)
axes[i].set_title('OS Usage in {0}'.format(zone))
axes[i].set_xlabel('Value')
axes[i].set_ylabel('Count')
fig.tight_layout()
fig.show()
产生下图:
注意:
希望这有帮助!