本文目录导读:

- Python + Matplotlib(最常用)
- Python + Plotly(交互式图表)
- JavaScript + Chart.js (Web图表)
- Bash + GnuPlot(命令行)
- 实用工具:从CSV生成图表
- 自动化脚本模板
- 使用建议
我来介绍几种生成数据曲线图的脚本方案,包括Python、JavaScript和命令行工具。
Python + Matplotlib(最常用)
基础折线图
import matplotlib.pyplot as plt
import numpy as np
# 生成数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# 创建图表
plt.figure(figsize=(10, 6))
plt.plot(x, y1, label='sin(x)', linewidth=2)
plt.plot(x, y2, label='cos(x)', linewidth=2)
和标签'正弦和余弦曲线', fontsize=14)
plt.xlabel('x 值', fontsize=12)
plt.ylabel('y 值', fontsize=12)
plt.legend()
plt.grid(True, alpha=0.3)
# 保存或显示
plt.savefig('curve.png', dpi=300, bbox_inches='tight')
plt.show()
多子图曲线
import matplotlib.pyplot as plt
import numpy as np
# 创建2x2子图
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 10))
x = np.linspace(0, 4*np.pi, 500)
# 子图1:基本曲线
ax1.plot(x, np.sin(x), 'b-', linewidth=2)
ax1.set_title('正弦波')
ax1.set_xlabel('时间')
ax1.set_ylabel('振幅')
# 子图2:多条曲线对比
ax2.plot(x, np.sin(x), 'r-', label='sin')
ax2.plot(x, np.cos(x), 'g--', label='cos')
ax2.legend()
ax2.set_title('sin vs cos')
# 子图3:散点+曲线
y = np.sin(x) + np.random.randn(500)*0.1
ax3.scatter(x, y, s=5, alpha=0.5)
ax3.plot(x, np.sin(x), 'r-', linewidth=2)
ax3.set_title('带噪声的数据')
# 子图4:填充区域
ax4.plot(x, np.sin(x))
ax4.fill_between(x, np.sin(x), 0, alpha=0.3)
ax4.set_title('填充曲线')
plt.tight_layout()
plt.show()
Python + Plotly(交互式图表)
import plotly.graph_objects as go
import numpy as np
# 生成数据
x = np.linspace(0, 20, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
# 创建交互式图表
fig = go.Figure()
# 添加曲线
fig.add_trace(go.Scatter(x=x, y=y1, mode='lines', name='sin'))
fig.add_trace(go.Scatter(x=x, y=y2, mode='lines', name='cos'))
fig.add_trace(go.Scatter(x=x, y=y3, mode='lines', name='tan'))
# 更新布局
fig.update_layout('交互式曲线图',
xaxis_title='X轴',
yaxis_title='Y轴',
hovermode='x'
)
# 保存为HTML(可交互)
fig.write_html('interactive_curve.html')
# 显示
fig.show()
JavaScript + Chart.js (Web图表)
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="myChart" style="max-width: 800px; margin: auto;"></canvas>
<script>
// 生成演示数据
const generateData = () => {
const data = [];
for (let i = 0; i < 50; i++) {
data.push({
x: i,
y: Math.sin(i/5) + Math.random()*0.2
});
}
return data;
};
const ctx = document.getElementById('myChart').getContext('2d');
const chart = new Chart(ctx, {
type: 'line',
data: {
labels: Array.from({length: 50}, (_, i) => i),
datasets: [{
label: '正弦波',
data: generateData().map(d => d.y),
borderColor: 'rgb(75, 192, 192)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
tension: 0.1
}]
},
options: {
responsive: true,
plugins: {
title: {
display: true,
text: '数据曲线图'
}
},
scales: {
y: {
beginAtZero: true
}
}
}
});
// 动态更新数据
setInterval(() => {
chart.data.datasets[0].data = generateData().map(d => d.y);
chart.update();
}, 2000);
</script>
</body>
</html>
Bash + GnuPlot(命令行)
#!/bin/bash
# 创建数据文件
cat > data.txt << EOF
1 5
2 3
3 8
4 6
5 11
6 9
7 13
8 17
EOF
# Gnuplot脚本
gnuplot << EOF
set terminal png size 800,600
set output 'gnuplot_curve.png''数据曲线图'
set xlabel 'X轴'
set ylabel 'Y轴'
set grid
# 绘制点图
plot 'data.txt' with points title '数据点' pt 7 ps 2 lc rgb 'blue', \
'data.txt' smooth bezier with lines title '平滑曲线' lw 2 lc rgb 'red'
EOF
echo "图表已生成: gnuplot_curve.png"
实用工具:从CSV生成图表
import pandas as pd
import matplotlib.pyplot as plt
import sys
def plot_csv(filepath, x_column, y_columns, title='数据曲线图'):
"""
从CSV文件生成曲线图
参数:
filepath: CSV文件路径
x_column: X轴列名
y_columns: Y轴列名列表 图表标题
"""
# 读取数据
df = pd.read_csv(filepath)
# 创建图表
plt.figure(figsize=(12, 6))
for col in y_columns:
plt.plot(df[x_column], df[col],
marker='o', markersize=3,
label=col, linewidth=1.5)
plt.title(title)
plt.xlabel(x_column)
plt.ylabel('值')
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
# 保存图表
output_file = 'output_curve.png'
plt.savefig(output_file, dpi=300)
print(f"图表已保存: {output_file}")
# 使用示例
if __name__ == "__main__":
# 创建演示数据
import numpy as np
data = {
'time': np.linspace(0, 10, 100),
'temperature': 20 + 10*np.sin(np.linspace(0, 2*np.pi, 100)),
'humidity': 50 + 20*np.cos(np.linspace(0, 2*np.pi, 100))
}
pd.DataFrame(data).to_csv('sample_data.csv', index=False)
# 生成图表
plot_csv('sample_data.csv', 'time',
['temperature', 'humidity'],
'环境和湿度变化')
自动化脚本模板
#!/usr/bin/env python3
"""
通用数据可视化脚本
用法: python plotter.py data.csv output.png
"""
import matplotlib.pyplot as plt
import pandas as pd
import sys
import os
class DataVisualizer:
def __init__(self, filepath):
self.df = pd.read_csv(filepath)
def generate_curve(self, x_col, y_cols, title, output_path):
"""生成曲线图"""
fig, ax = plt.subplots(figsize=(12, 6))
for y_col in y_cols:
ax.plot(self.df[x_col], self.df[y_col],
label=y_col, linewidth=2)
ax.set_xlabel(x_col)
ax.set_ylabel('值')
ax.set_title(title)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig(output_path, dpi=300)
plt.close()
print(f"✓ 图表已生成: {output_path}")
def generate_multi_plot(self, config):
"""生成多图"""
n = len(config)
fig, axes = plt.subplots(1, n, figsize=(6*n, 4))
for i, (name, y_col) in enumerate(config.items()):
axes[i].plot(self.df.index, self.df[y_col])
axes[i].set_title(name)
axes[i].grid(True)
plt.tight_layout()
return fig
# 主程序
if __name__ == "__main__":
if len(sys.argv) < 2:
print("用法: python plotter.py data.csv [output.png]")
sys.exit(1)
data_file = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) > 2 else 'output.png'
# 创建可视化器
visualizer = DataVisualizer(data_file)
# 生成图表
visualizer.generate_curve(
x_col='x', # 根据实际数据修改
y_cols=['y1', 'y2'], # 根据实际数据修改
title='自动生成的数据曲线',
output_path=output_file
)
使用建议
- 快速可视化:使用Matplotlib(Python)或GnuPlot(命令行)
- 交互式需求:Plotly(Python)或Chart.js(Web)
- 数据探索:建议使用Jupyter Notebook + Matplotlib
- 自动化报表:使用上述脚本模板批量处理
- 大数据量:考虑使用seaborn或bokeh优化性能
这些脚本可以直接复制使用,只需根据你的数据格式调整参数即可,需要更具体的定制吗?
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播