matplotlib 绘制折现图

本文通过使用Python的Matplotlib库,模拟并绘制了海南一天24小时内的气温变化曲线图,展示了如何生成随机温度数据,并将其可视化为易于理解的图表。
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import random
# 保证生成的图片在浏览器内显示
#%matplotlib inline
# 保证能正常显示中文(Mac)
#plt.rcParams['font.family'] = ['Arial Unicode MS']

# 模拟海南一天的温度变化

# 生成x轴的24小时
hainan_x = [h for h in range(0, 24)]

# 生成y轴的温度随机值(15, 25)
hainan_y = [random.randint(15, 25) for t in range(0, 24)]

# 设置画板属性
plt.figure(figsize = (12, 6), dpi = 100)

# 往画板绘图
plt.plot(hainan_x, hainan_y, label="海南")

## 生成24小时的描述
x_ = [x_ for x_ in range(0, 24)]
x_desc = ["{}时".format(x_desc) for x_desc in x_]
#
## 设置x轴显示 24小时
plt.xticks(x_, x_desc)
#
## 生成0至30度的描述
#y_ = [y_ for y_ in range(0, 30)][::2]
#y_desc = ["{}℃".format(y_desc) for y_desc in y_]


# 设置y轴显示温度描述
#plt.yticks(y_, y_desc)

# 指定x y轴的名称
plt.xlabel("时间")
plt.ylabel("温度")

# 指定标题
plt.title("一天内温度的变化")

# 显示图例
plt.legend(loc="best")
 
# 将数据生成图片, 保存到当前目录下
plt.savefig("./t.png")
# 在浏览器内展示图片
plt.show()


在这里插入图片描述

### 使用 Python Matplotlib绘制四条折线图 为了实现使用 `matplotlib` 绘制四条折线图的功能,可以扩展给定的代码逻辑。以下是完整的解决方案: #### 1. 数据准备 首先需要定义每一条折线的数据点集合。这些数据可以通过随机生成或其他方式提供。 #### 2. 折线样式配置 通过调整线条的颜色、风格(实线/虚线)、标记符以及其他属性来区分不同的折线。 #### 3. 图形显示设置 利用 `plt.xlabel()` 和 `plt.ylabel()` 设置坐标轴标签;用 `plt.title()` 添加图表标题;最后调用 `plt.legend()` 显示图例以便识别各条折线。 下面是具体的示例代码: ```python import matplotlib.pyplot as plt import random # 准备数据 x_data = list(range(1, 31)) # X 轴数据范围为 1 到 30 的整数序列 y_view_1 = [random.randint(50, 100) for _ in range(30)] y_view_2 = [random.randint(100, 150) for _ in range(30)] y_view_3 = [random.randint(150, 200) for _ in range(30)] y_view_4 = [random.randint(200, 250) for _ in range(30)] # 创建绘图对象并绘制多条折线 plt.figure(figsize=(10, 6)) plt.plot(x_data, y_view_1, linestyle='-', color='blue', marker='o', label='Line 1') plt.plot(x_data, y_view_2, linestyle='--', color='green', marker='s', label='Line 2') plt.plot(x_data, y_view_3, linestyle='-.', color='red', marker='^', label='Line 3') plt.plot(x_data, y_view_4, linestyle=':', color='purple', marker='*', label='Line 4') # 配置图形细节 plt.title('Four Line Chart Example with Matplotlib', fontsize=16) plt.xlabel('X-axis (Days)', fontsize=14) plt.ylabel('Y-axis (Values)', fontsize=14) # 增加网格辅助观察趋势 plt.grid(True, linestyle='--', alpha=0.7) # 展示图例 plt.legend(fontsize=12) # 显示最终结果 plt.show() ``` 此代码片段展示了如何创建具有不同样式的四条折线,并且包含了必要的标注和美化选项[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值