在Python中创建条形图

在Python中创建条形图,python,charts,matplotlib,bar-chart,Python,Charts,Matplotlib,Bar Chart,我试图用python创建的条形图有几个问题。我的图表代码如下所示: import matplotlib matplotlib.use('Agg') from pylab import * import calendar def webshow(img): savefig(img,dpi=500) print 'Content-Type: text/html\n' print '<img width="800" height="400" src="'+img+'"

我试图用python创建的条形图有几个问题。我的图表代码如下所示:

import matplotlib
matplotlib.use('Agg')
from pylab import *
import calendar

def webshow(img):
    savefig(img,dpi=500)
    print 'Content-Type: text/html\n'
    print '<img width="800" height="400" src="'+img+'" />'

genres = []
n = 0
for c in sorted_list: 
    genres.append(sorted_list[n][0])
    n += 1

grosses = []
a = 0
for c in sorted_list:
    grosses.append(sorted_list[a][1])
    a += 1

clf()
bar(arange(len(grosses)),grosses)
xticks( arange(len(genres)),genres, rotation=80)
webshow("barchart.png")
导入matplotlib
matplotlib.use('Agg')
从派拉布进口*
导入日历
def webshow(img):
保存图(img,dpi=500)
打印“内容类型:text/html\n”
打印“
类型=[]
n=0
对于排序列表中的c:
追加(已排序的\u列表[n][0])
n+=1
总收入=[]
a=0
对于排序列表中的c:
附加(已排序的列表[a][1])
a+=1
clf()
酒吧(阿兰奇(莱恩(格罗斯)),格罗斯)
xticks(arange(len(体裁)),体裁,轮换=80)
webshow(“barchart.png”)
我的图表如下所示:

import matplotlib
matplotlib.use('Agg')
from pylab import *
import calendar

def webshow(img):
    savefig(img,dpi=500)
    print 'Content-Type: text/html\n'
    print '<img width="800" height="400" src="'+img+'" />'

genres = []
n = 0
for c in sorted_list: 
    genres.append(sorted_list[n][0])
    n += 1

grosses = []
a = 0
for c in sorted_list:
    grosses.append(sorted_list[a][1])
    a += 1

clf()
bar(arange(len(grosses)),grosses)
xticks( arange(len(genres)),genres, rotation=80)
webshow("barchart.png")

(来源:)


基本上,我的主要问题是这些值是用科学记数法表示的小数。如果可能的话,我想把它们以百万计呈现出来。此外,我不知道如何使它,这样的体裁是不切断在底部。谢谢你的帮助

首先,我会使用一个
对象来处理:这样可以更容易地根据您的喜好构建绘图。要构建图形,应执行以下操作:

fig = plt.figure()
fig.subplots_adjust(bottom=0.2)          # Remark 1
ax = fig.add_subplot(111)
ax.bar(arange(len(grosses)), grosses)
ax.ticklabel_format(style='plain')       # Remark 2
ax.set_xticks(arange(len(genres)))
ax.set_xticklabels(genres, rotation=80)
savefig('barchart.png', dpi=500)
连同以下评论:

  • 这会调整图像的大小,在这种情况下,它会放大底部以适合标签。在大多数情况下,您大致知道要输入的数据,因此这就足够了。如果您使用的是matplotlib 1.1版或更高版本,则可以使用该函数自动执行此操作。另一种方法是根据所有轴标签的边界框自行计算所需的大小,如图所示,但需要渲染器才能确定所有标签的大小
  • 通过指定标签格式(使用
    sci
    plain
    ),可以更改值的呈现方式。当使用
    普通
    时,它只会按原样呈现值。有关更多信息,请参阅此功能上的。请注意,还可以使用函数的
    text
    参数进一步控制格式(当然,该函数也可用于x轴)