亲宝软件园·资讯

展开

Python 图形绘制

来西瓜 人气:0

4、条形图

下面介绍条形图的画法。

4.1 代码

import matplotlib.pyplot as plt



# x-coordinates of left sides of bars

left = [1, 2, 3, 4, 5]



# heights of bars

height = [10, 24, 36, 40, 5]



# labels for bars

tick_label = ['one', 'two', 'three', 'four', 'five']



# plotting a bar chart

plt.bar(left, height, tick_label = tick_label,

        width = 0.8, color = ['red', 'green'])



# naming the x-axis

plt.xlabel('x - axis')

# naming the y-axis

plt.ylabel('y - axis')

# plot title

plt.title('My bar chart!')



# function to show the plot

plt.show()



4.2 输出

4.3 代码的部分解释

5、直方图

5.1 代码

import matplotlib.pyplot as plt



# frequencies

ages = [2,5,70,40,30,45,50,45,43,40,44,

        60,7,13,57,18,90,77,32,21,20,40]



# setting the ranges and no. of intervals

range = (0, 100)

bins = 10 



# plotting a histogram

plt.hist(ages, bins, range, color = 'green',

        histtype = 'bar', rwidth = 0.8)



# x-axis label

plt.xlabel('age')

# frequency label

plt.ylabel('No. of people')

# plot title

plt.title('My histogram')



# function to show the plot

plt.show()



5.2 输出

5.3 代码的部分解释

6、散点图

6.1 代码

import matplotlib.pyplot as plt



# x-axis values

x = [1,2,3,4,5,6,7,8,9,10]

# y-axis values

y = [2,4,5,7,6,8,9,11,12,12]



# plotting points as a scatter plot

plt.scatter(x, y, label= "stars", color= "green",

            marker= "*", s=30)



# x-axis label

plt.xlabel('x - axis')

# frequency label

plt.ylabel('y - axis')

# plot title

plt.title('My scatter plot!')

# showing legend

plt.legend()



# function to show the plot

plt.show()



6.2 输出

6.3 代码的部分解释

加载全部内容

相关教程
猜你喜欢
用户评论