在Python中创建条形图
我试图用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+'"
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)
连同以下评论:
sci
或plain
),可以更改值的呈现方式。当使用普通
时,它只会按原样呈现值。有关更多信息,请参阅此功能上的。请注意,还可以使用函数的text
参数进一步控制格式(当然,该函数也可用于x轴)