面向对象api直方图,饼图,散点图,误差棒图

直方图:

import matplotlib.pyplot as plt
import numpy as np


# 1.创建画布
fig = plt.figure()

#2. 在画布上添加绘图区域
ax = fig.add_subplot(111)

# 3.调用绘图方法绘制图表

# (1)准备数据
data = np.random.randint(0,100,50)
ax.hist(data,bins=8,histtype='stepfilled')

# (2)展示图表
plt.show()

饼图 

# 1.创建画布
fig = plt.figure()


# 2.在画布上添加绘图区域
ax = fig.add_subplot(111)

# 3.准备数据
data = np.array([30, 50, 10, 15, 40, 65])
pie_labels = np.array(['E', 'B', 'C', 'F', 'G', 'H'])
explode_position = [0.05, 0.05, 0.05, 0.05, 0.05, 0.05]


# 4.调用绘图方法绘制图表
ax.pie(data, explode=explode_position, radius=1, labels=pie_labels, autopct='%3.1f%%',shadow=True)


# 5.展示图表
plt.show()
 

散点图 

import matplotlib.pyplot as plt
import numpy as np

# 1.创建画布
fig = plt.figure()

# 2.在画布上添加绘图区域
ax = fig.add_subplot(111)

# 3.准备数据
num = 60
x_data = np.random.rand(num)
y_data = np.random.rand(num)

# 4.调用绘图方法绘制图表
ax.scatter(x_data,y_data)

# 5.展示图表
plt.show()

误差棒图: 

import matplotlib.pyplot as plt
import numpy as np

# 1.显示中文
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

# 2.创建画布
fig = plt.figure()
# 3.在画布上添加绘图区域
ax = fig.add_subplot(111)

# 4.准备数据
# (1)准备 x 轴和 y 轴的数据
x = np.arange(3)
y1 = np.array([3.05, 1.55, 1.22])
y2 = np.array([1.29, 1.61, 1.64])
y3 = np.array([4.35, 4.55, 4.64])
y4 = np.array([3.59, 3.33, 3.20])

# 5.指定测量偏差
error1 = [0.16, 0.08, 0.10]
error2 = [0.27, 0.14, 0.14]
error3 = [0.34, 0.32, 0.29]
error4 = [0.23, 0.23, 0.39]
bar_width = 0.2

# 6.调用绘图方法绘制图表
width = 0.2
ax.bar(x, y1,width=bar_width)
ax.bar(x+width, y2,align='center',tick_label=['冬季','夏季','秋季'],width=bar_width)
ax.bar(x+2*width, y3,width=bar_width)
ax.bar(x+3*width, y4,width=bar_width)

# 7.添加误差棒
ax.errorbar(x,y1,yerr=error1,capsize=4,capthick=1,fmt='k,')
ax.errorbar(x+width,y2,yerr=error2,capsize=4,capthick=1,fmt='k,')
ax.errorbar(x+2*width,y3,yerr=error3,capsize=4,capthick=1,fmt='k,')
ax.errorbar(x+3*width,y4,yerr=error4,capsize=4,capthick=1,fmt='k,')

# 8.展示图表
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是对于这个问题的回答,包括对数据进行基本探索性分析和绘制常用的形。 1. 下载数据文件 从国家统计局官网下载近二十年旅客运距数据csv文件,方法如上面所述。 2. 导入数据文件 使用R语言的read.csv函数读取下载下来的数据文件,代码如下: ```{r} library(tidyverse) data <- read.csv("路径/文件名.csv", header = TRUE, encoding = "UTF-8") ``` 3. 探索性分析 接下来对数据进行初步的探索性分析,包括以下几个方面: - 查看数据结构和摘要 - 查看数据的分布情况 - 查看数据的相关性 代码如下: ```{r} # 查看数据结构和摘要 str(data) summary(data) # 查看数据的分布情况 ggplot(data, aes(x = 旅客运距)) + geom_histogram(binwidth = 500) + labs(title = "旅客运距分布直方图", x = "旅客运距", y = "频数") # 查看数据的相关性 ggplot(data, aes(x = 年份, y = 旅客运距)) + geom_point() + geom_smooth(method = "lm") + labs(title = "年份与旅客运距相关性分析", x = "年份", y = "旅客运距") ``` 4. 绘制基本形 接下来我们绘制基本的形,包括直方图饼图散点图等。代码如下: ```{r} # 直方图 ggplot(data, aes(x = 旅客运距)) + geom_histogram(binwidth = 500) + labs(title = "旅客运距分布直方图", x = "旅客运距", y = "频数") # 饼图 data %>% group_by(类型) %>% summarise(count = n()) %>% ggplot(aes(x = "", y = count, fill = 类型)) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y") + labs(title = "旅客运距数据分类情况", fill = "类型") # 散点图 ggplot(data, aes(x = 年份, y = 旅客运距)) + geom_point() + geom_smooth(method = "lm") + labs(title = "年份与旅客运距相关性分析", x = "年份", y = "旅客运距") ``` 希望这个回答能够帮到你,如果还有其他问题,欢迎继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值