简体   繁体   English

Python Pyplot Bar使用对数刻度时,绘图条消失

[英]Python Pyplot Bar Plot bars disappear when using log scale

I have the following data: 我有以下数据:

20120219,\\n,43166053
20120220,\\n,46813269
20120221,\\n,47277204
20120222,\\n,46344556
20120223,\\n,26926236
20120224,\\n,6472506
20120225,\\n,39580476
20120226,\\n,55968342
20120227,\\n,32889948
20120228,\\n,32116361
20120229,\\n,32424829
20120301,\\n,56123889
20120302,\\n,67102459
20120303,\\n,81681885
20120304,\\n,85740021
20120305,\\n,83874668
20120306,\\n,83606683
20120307,\\n,56660981
20120308,\\n,44534668
20120309,\\n,37532071
20120310,\\n,39260242
20120311,\\n,40491186
20120312,\\n,39041085
20120313,\\n,27010562
20120314,\\n,44121900
20120315,\\n,87750645
20120316,\\n,86588523
20120317,\\n,86121469
20120318,\\n,89343506
20120319,\\n,89198664
20120320,\\n,90273127

I have the following code to create a bar plot: 我有以下代码来创建条形图:

import matplotlib
matplotlib.use('Agg')
from matplotlib.mlab import csv2rec
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from pylab import *
from datetime import datetime
import dateutil
import sys
import matplotlib.ticker as mticker

y = []                                                                                                    

input = open(sys.argv[1], 'r')                                                                                                                                          

data = csv2rec(input, names=['date', 'symbol', 'count'])                      

for item in data['count']:
        y.append(item)

time_vec = [datetime.strptime(str(x),'%Y%m%d') for x in data['date']]

rcParams['figure.figsize'] = 18, 7
rcParams['font.size'] = 8

N = len(y)

ind = range(N)

fig = plt.figure()                              # Setup the figure

ax = fig.add_subplot(111)                       # Setup the subplot
ax.bar(ind, y, align='center')                  # Setup the Y Bars

ax.set_ylabel('Counts') 

ax.set_title('Collected By Day')               

ax.set_xticks(ind)
ax.set_xticklabels(time_vec)
ax.grid()                                      

fig.autofmt_xdate(bottom=0.2, rotation=90, ha='left')

plt.yscale('log')

plt.savefig(str(sys.argv[1] + ".png"))

This creates the following graph: 这将创建以下图表:

日条形图

If I remove the "plt.yscale('log')" line then I get full bars. 如果我删除“plt.yscale('log')”行,那么我会得到完整的条形图。 However because I want to add other lines I really need this to have a logarithmic y scale. 但是因为我想添加其他行我真的需要这个以对数y比例。 Based on some searching I believe this is a bug in pyplot however I can't seem to find a resolution. 根据一些搜索,我相信这是pyplot中的一个错误,但我似乎无法找到解决方案。 Does anyone know how to get a logarithmic bar plot to actually show the bars? 有谁知道如何获得一个对数条形图来实际显示条形图?

bar函数有一个log参数,在使用对数标度时需要设置为true: http//matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM