在绘图时,熊猫省略了第一行

时间:2016-12-23 15:38:43

标签: python python-2.7 pandas plot

我有这个代码,在这个例子中,它应该根据datetime,months来计算值的数量。

# -*- coding: utf-8 -*-
"""
Created on Mon Nov 28 13:31:33 2016

@author: ...
"""
import sys  
reload(sys)  
sys.setdefaultencoding('utf8')

import pandas as pd
import matplotlib.pyplot as plt

"""read and count"""
df = pd.read_csv('meteo_merged.csv', parse_dates=[1])
df['datetime'] = df.datetime.apply(lambda x: x.month)

x = df.groupby(['datetime']).count()
df2 = pd.read_csv('policja.csv', parse_dates=[1])
df2['datetime'] = df2.datetime.apply(lambda x: x.month)
p = df2.groupby('datetime').count()

"""plot"""
ax = x.plot(y='station', kind='bar', legend=False)
ax2 = p.plot(ax=ax, color='g', secondary_y=True, legend=False, rot=60, figsize=(8,6))
ax2.lines[0].set_linewidth(3)

"""axes labels etc."""
godziny = ['styczeń', 'luty', 'marzec', 'kwiecień', 'maj', 'czerwiec', 'lipiec', 'sierpień', 'wrzesień', 'październik', 'listopad', 'grudzień']
ax.set_xticklabels(godziny)
ax.set_ylabel('Liczba dni z ograniczoną widocznością dla danego miesiąca').set_color('blue')
ax2.set_ylabel('Liczba zdarzeń drogowych w danym miesiącu').set_color('green')
ax.set_xlabel('Miesi\xc4\x84c')

"""ticks settings"""
ax.spines['left'].set_color('blue')
ax.spines['right'].set_color('green')
ax.tick_params(axis='y', colors='blue')
ax2.tick_params(axis='y', colors='green')

fig = plt.gcf()
fig.savefig('mgla_miesiace.svg', format='svg', dpi=300)

X轴从第二个月开始,但DataFrame有12行:

       station  humidity
datetime                   
1           15101     15101
2           11171     11171
3           12057     12057
4           13031     13031
5           19835     19835
6           17421     17421
7           18145     18145
8           17980     17980
9           21347     21347
10          22050     22050
11          22265     22265
12          11279     1127>> >9

Plot result:

我有一个非常相似的代码,只做了几个小时,它工作正常,每一行都被绘制。唯一的区别是行:

df['datetime'] = df.datetime.apply(lambda x: x.hour)

我不知道这里有什么问题。此外,编码似乎存在问题。无论我怎样尝试,我都不能画出诸如ą,ę,ó等字母。

1 个答案:

答案 0 :(得分:0)

对于您的字体问题,可以通过强制matplotlib使用其他字体来修复它。例如,如果你正在使用Ubuntu,你可以试试Liberation Serif

matplotlib.rc('font', family='Liberation Serif')

当然,只要它支持你想要的字符,你可以根据你想要的字体来改变它。

对于月份问题,您是否尝试使用plt.axis扩展轴?