Matplotlib数据图包含太多标签

时间:2015-07-08 14:04:27

标签: python pandas matplotlib

我尝试使用3D图形来显示csv数据。

我的代码包含在下面:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d


MY_FILE = 'total_watt.csv'

df = pd.read_csv(MY_FILE, parse_dates=[0], header=None, names=['datetime', 'consumption'])

df['date'] = [x.date() for x in df['datetime']]
df['time'] = [x.time() for x in df['datetime']]

pv = df.pivot(index='time', columns='date', values='consumption')

# to avoid holes in the surface
pv = pv.fillna(0.0)

xx, yy = np.mgrid[0:len(pv),0:len(pv.columns)]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

surf=ax.plot_surface(xx, yy, pv.values, cmap='jet', cstride=1, rstride=1)

fig.colorbar(surf, shrink=0.5, aspect=10)

dates = [x.strftime('%m-%d') for x in pv.columns]
times = [x.strftime('%H:%M') for x in pv.index]
consumptions = [x for x in pv.values]

ax.set_title('Energy consumptions Clusters')
ax.set_xlabel('time', color='lightseagreen')
ax.set_ylabel('date(year 2011)', color='lightseagreen')
ax.set_zlabel('energy consumption', color='lightseagreen')
ax.set_xticks(xx[::10,0])
ax.set_xticklabels(times[::10], color='lightseagreen')
ax.set_yticks(yy[0,::10])
ax.set_yticklabels(dates[::10], color='lightseagreen')
ax.set_zticklabels(consumptions[::100000], color='lightseagreen')

ax.set_axis_bgcolor('black')



plt.show()

虽然我成功上色了;

x轴ax.set_xlabel('time', color='lightseagreen')

y轴ax.set_ylabel('date(year 2011)', color='lightseagreen')

z轴ax.set_zlabel('energy consumption', color='lightseagreen')

x-axis ax.set_xticklabels(times[::10], color='lightseagreen')

中打勾

y-axis ax.set_yticklabels(dates[::10], color='lightseagreen')

中打勾

我无法正确地在z轴上打勾,因为x轴绘制了'时间'和y轴绘图日期,这些都是正确定义的。我认为我定义消费的方式(consumptions = [x for x in pv.values])是不正确的,它会导致这个错误..

我从这段代码得到的3d图是

我的问题是什么原因以及如何解决?

1 个答案:

答案 0 :(得分:0)

我不确定是否会出现问题的目的而无法测试,但我认为您应该更改zticks列表的长度:

#one.rb

puts "hello"

抱歉,我无法检查它是否有效