"""
@File : create_pic.py
@Time : 2022/10/24 17:13:16
@Author : JuYongkang
@Version : 1.0
@Contact : j_juyongkang@163.com
@Desc : 读取csv数据生成双Y轴折线图
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import time
import matplotlib as mpl
import os
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 设置简黑字体
mpl.rcParams['axes.unicode_minus'] = False
plt.rcParams.update({'figure.max_open_warning': 0})
def main(data):
# 读取csv文件
df = pd.read_csv(data)
# 生成一维数组
times = df['Time'].tolist()
speed = df['Wind Speed 150m'].tolist()
direction = df['Direction 150m'].tolist()
# 建立单轴
fig, ax1 = plt.subplots(1, 1, figsize=(20, 10))
ax1.plot(times, speed, color='green', alpha=0.9, marker='', markersize=2, label='Speed 150m')
# 设置轴label及X轴密度
ax1.xaxis.set_major_locator(ticker.MultipleLocator(base=30))
ax1.set_xlabel('Date/Time', fontsize=20)
ax1.set_ylabel('Speed 100m', fontsize=20)
# 设置Y轴范围
ax1.set_yticks(range(0, 50, 10))
# 设置Y轴字体大小
ax1.tick_params(labelsize=20)
# X轴字体旋转角度
# plt.xticks(rotation=45)
# 图例位置
plt.legend(bbox_to_anchor=(0, 0.98), loc='upper left', fontsize=20, frameon=True)
# 建立第二个Y轴
ax2 = ax1.twinx()
ax2.plot(times, direction, color='red', alpha=0.9, marker='', markersize=2, label='Direction 150')
ax2.set_ylabel('Direction 100', fontsize=20)
ax2.set_yticks(range(0, 300, 50))
ax2.tick_params(labelsize=20)
plt.legend(bbox_to_anchor=(1, 0.98), loc='upper right', fontsize=20, frameon=True)
# 展示图片
# plt.show()
# 保存图片(去除白边)
plt.savefig(r'C:\Users\JK\Documents\Projects\Modbus\DealData\三峡阳江1104.jpg', bbox_inches='tight')
# 保存图片(带白边)
# plt.savefig(r'C:\Users\JK\Documents\Projects\Modbus\DealData\湛江_带白边.jpg')
#
if __name__ == '__main__':
data = '三峡阳江1104.csv'
main(data)
![python 读取csv数据生成双Y轴折线图](/upload/2022/11/wallhaven-ymgpkd_1920x1080.png)
python 读取csv数据生成双Y轴折线图
Fre_soe
260
2022-11-07