Python 熊猫在绘图时忽略第一行
我有这段代码,在本例中,它应该根据datetime、月份来计算值的数量Python 熊猫在绘图时忽略第一行,python,python-2.7,pandas,plot,Python,Python 2.7,Pandas,Plot,我有这段代码,在本例中,它应该根据datetime、月份来计算值的数量 # -*- 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"""
# -*- 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
我有一个非常相似的代码,它做同样的事情,只有几个小时,它工作得很好,每一行都被绘制出来。唯一的区别是线条:
df['datetime'] = df.datetime.apply(lambda x: x.hour)
我不知道这里有什么问题。此外,编码似乎也有问题。无论我怎么尝试,我都无法绘制字母,如ą、ę、ó等。对于字体问题,可以通过强制matplotlib使用其他字体来解决。例如,如果你正在使用Ubuntu,你可以试试解放Serif
matplotlib.rc('font', family='Liberation Serif')
当然,你可以根据你想要的字体来改变它,只要它支持你想要的字符
对于月份问题,您是否尝试过使用plt扩展轴?axis应在注释中提出问题,而不是添加plt。xlim([0,12])使其显示12条,但绘图被替换。两个数据帧中的值彼此不对应。仅绘制不带辅助y的原始条形图可获得预期结果,不缺少任何数据。