Python 从数据透视表打印数据
我基本上是在复制气候图,显示不同地区全年的平均温度和降水量 我通过以下方式从我的csv生成了一个透视表:Python 从数据透视表打印数据,python,python-3.x,pandas,matplotlib,pivot-table,Python,Python 3.x,Pandas,Matplotlib,Pivot Table,我基本上是在复制气候图,显示不同地区全年的平均温度和降水量 我通过以下方式从我的csv生成了一个透视表: data = pd.read_csv("05_temp_rain_v2.csv") pivot = data.pivot_table(["rain(mm)","temp(dC)"], ["loc","month"]) 文本形式的示例数据: loc,lat,long,year,month,rain(mm),temp(dC) Adria_-_Bellombra,45.011129,12.0
data = pd.read_csv("05_temp_rain_v2.csv")
pivot = data.pivot_table(["rain(mm)","temp(dC)"], ["loc","month"])
文本形式的示例数据:
loc,lat,long,year,month,rain(mm),temp(dC)
Adria_-_Bellombra,45.011129,12.034126,1994,1,45.6,4.6
Adria_-_Bellombra,45.011129,12.034126,1994,2,31.4,4
Adria_-_Bellombra,45.011129,12.034126,1994,3,1.6,10.7
Adria_-_Bellombra,45.011129,12.034126,1994,4,74.4,11.5
Adria_-_Bellombra,45.011129,12.034126,1994,5,26,17.2
Adria_-_Bellombra,45.011129,12.034126,1994,6,108.6,20.6
数据透视表:
由于我在处理不同的位置,因此我会对它们进行迭代:
locations=pivot.index.get_level_values(0).unique()
for location in locations:
split=pivot.xs(location)
rain=split["rain(mm)"]
temp=split["temp(dC)"]
plt.subplots()
temp.plot(kind="line",color="r",).legend()
rain.plot(kind="bar").legend()
图输出示例如下所示:
为什么从2月(2日)开始绘制我的温度值?我认为这是因为温度值列在第二列中
处理和绘制数据透视表中不同数据(两列)的正确方法是什么 您可以循环查看
groupby
操作的结果:
for name, group in data[['loc', 'month', 'rain(mm)', 'temp(dC)']].groupby('loc'):
group.set_index('month').plot()
这是因为
line
和bar
绘图没有以相同的方式设置xlim
。对于条形图,x轴被解释为分类数据,而对于线形图,x轴被解释为连续数据。结果是,xlim
和xtick
在这两种情况下设置不相同
考虑这一点:
In [4]: temp.plot(kind="line",color="r",)
Out[4]: <matplotlib.axes._subplots.AxesSubplot at 0x117f555d0>
In [5]: plt.xticks()
Out[5]: (array([ 1., 2., 3., 4., 5., 6.]), <a list of 6 Text xticklabel objects>)
作者:
谢谢你,我想我终于很满意了
for location in locations:
#print(pivot.xs(location, level=0))
split=pivot.xs(location)
rain=split["rain(mm)"]
temp=split["temp(dC)"]
fig = plt.figure()
ax1 = rain.plot(kind="bar")
ax2 = ax1.twinx()
ax2.plot(ax1.get_xticks(),temp,linestyle='-',color="r")
ax2.set_ylim((-5, 50.))
#ax1.set_ylim((0, 300.))
ax1.set_ylabel('Precipitation (mm)', color='blue')
ax2.set_ylabel('Temperature (°C)', color='red')
ax1.set_xlabel('Months')
plt.title(location)
labels = ['Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dez']
#plt.xticks(range(12),labels,rotation=45)
ax1.set_xticklabels(labels, rotation=45)
我收到以下输出,与我的预期非常接近:
如果您能以文本形式提供示例数据,您将更容易获得帮助!添加了示例数据。一个更优雅的选项是直接绘制
groupby
对象,但我不确定如何使其准确工作:data.groupby('loc')。plot()
+,这是一个很好的解决方案!有没有办法对任何x轴(任何类型的数据)执行范围(6)
技巧?我是说一个普遍的解决方案。。。也许可以节省xticks或者只是它的长度?@MaxU,谢谢,修复了你的情节仍然比我的好;)谢谢你的澄清!
temp.plot(kind="line", color="r",).legend()
rain.plot(kind="bar").legend()
rain.plot(kind="bar").legend()
plt.plot(range(len(temp)), temp, "r", label=temp.name)
plt.legend()
for location in locations:
#print(pivot.xs(location, level=0))
split=pivot.xs(location)
rain=split["rain(mm)"]
temp=split["temp(dC)"]
fig = plt.figure()
ax1 = rain.plot(kind="bar")
ax2 = ax1.twinx()
ax2.plot(ax1.get_xticks(),temp,linestyle='-',color="r")
ax2.set_ylim((-5, 50.))
#ax1.set_ylim((0, 300.))
ax1.set_ylabel('Precipitation (mm)', color='blue')
ax2.set_ylabel('Temperature (°C)', color='red')
ax1.set_xlabel('Months')
plt.title(location)
labels = ['Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dez']
#plt.xticks(range(12),labels,rotation=45)
ax1.set_xticklabels(labels, rotation=45)