<!-- <!DOCTYPE html>
<html>
<head>
<title>视觉映射和图例</title>
<meta charset="utf-8">
<script src="echarts.js"></script>
</head>
<body>
<div style="width: 600px;height:400px;">
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('main'));
var option =
{
legend: {
// Try 'horizontal'
orient: 'vertical',
right: 10,
top: 20,
bottom: 20,
type: 'scroll',
backgroundColor: '#000',
textStyle:{
color:'#FFF',
},
icon: 'square',
},
dataset: {
source: [
['product', '2015', '2016', '2017', '2018', '2019', ],
['Matcha Latte', 43.3, 85.8, 93.7, 43.3, 85.8, 93.7, 72.4, 53.9, 39.1],
['Milk Tea', 83.1, 73.4, 55.1, 83.1, 73.4, 55.1, 72.4, 53.9, 39.1],
['Cheese Cocoa', 86.4, 65.2, 82.5, 86.4, 65.2, 82.5, 72.4, 53.9, 39.1],
['Walnut Brownie', 72.4, 53.9, 39.1, 72.4, 53.9, 39.1, 72.4, 53.9, 39.1]
]
},
xAxis: { type: 'category' },
yAxis: {},
series: [{ type: 'line' }, { type: 'line' }, { type: 'line' }, { type: 'line' }, { type: 'line' }, { type: 'line' },]
};
myChart.setOption(option);
</script>
</div>
</body>
</html>
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<!-- 引入刚刚下载的 ECharts 文件 -->
<script src="echarts.js"></script>
</head>
<body>
<div id="main" style="width: 600px;height:400px;">
<script type="text/javascript">
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'),'purple-passion');
// 指定图表的配置项和数据
var option =
{
legend: {
// Try 'horizontal'
orient: 'vertical',
right: 10,
top: 20,
bottom: 20,
type: 'scroll',
backgroundColor: '#000',
textStyle:{
color:'#FFF',
},
icon: 'square',
},
dataset: {
source: [
['product', '2015', '2016', '2017', '2018', '2019', ],
['Matcha Latte', 43.3, 85.8, 93.7, 43.3, 85.8, 93.7, 72.4, 53.9, 39.1],
['Milk Tea', 83.1, 73.4, 55.1, 83.1, 73.4, 55.1, 72.4, 53.9, 39.1],
['Cheese Cocoa', 86.4, 65.2, 82.5, 86.4, 65.2, 82.5, 72.4, 53.9, 39.1],
['Walnut Brownie', 72.4, 53.9, 39.1, 72.4, 53.9, 39.1, 72.4, 53.9, 39.1]
]
},
xAxis: { type: 'category' },
yAxis: {},
series: [{ type: 'line' }, { type: 'line' }, { type: 'line' }, { type: 'line' }, { type: 'line' }, { type: 'line' },]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>多图表示例</title>
<!-- 引入 ECharts 文件 -->
<script src="echarts.js"></script>
</head>
<body>
<!-- 柱状图容器 -->
<div id="barChart" style="width: 400px; height: 300px; float: left;"></div>
<!-- 条形图容器 -->
<div id="barHorizontalChart" style="width: 400px; height: 300px; float: left;"></div>
<!-- 折线图容器 -->
<div id="lineChart" style="width: 400px; height: 300px; float: left;"></div>
<!-- 饼图容器 -->
<div id="pieChart" style="width: 400px; height: 300px; float: left;"></div>
<!-- 雷达图容器 -->
<div id="radarChart" style="width: 400px; height: 300px; float: left;"></div>
<script type="text/javascript">
var barChart = echarts.init(document.getElementById('barChart'));
var barHorizontalChart = echarts.init(document.getElementById('barHorizontalChart'));
var lineChart = echarts.init(document.getElementById('lineChart'));
var pieChart = echarts.init(document.getElementById('pieChart'));
var radarChart = echarts.init(document.getElementById('radarChart'));
// 柱状图配置项
var barOption = {
xAxis: { type: 'category',data: ['A', 'B', 'C', 'D', 'E', 'F'] },
yAxis: { type: 'value' },
series: [{ data: [210, 320, 280, 330, 350, 420], type: 'bar' }]
};
// 条形图配置项
var barHorizontalOption = {
yAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E', 'F'] },
xAxis: { type: 'value' },
series: [{ data: [210, 320, 280, 330, 350, 420], type: 'bar' }]
};
// 折线图配置项
var lineOption = {
xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] },
yAxis: { type: 'value' },
series: [{ data: [120, 200, 150, 80, 70, 110, 130], type: 'line' }]
};
// 饼图配置项
var pieOption = {
series: [{ data: [ {value: 335, name: 'A'},
{value: 310, name: 'B'},
{value: 234, name: 'C'},
{value: 135, name: 'D'},
{value: 1548, name: 'E'}
],
type: 'pie' }]
};
// 雷达图配置项
var radarOption = { radar: { indicator: [ {name: 'A', max: 6500},
{name: 'B', max: 16000},
{name: 'C', max: 30000},
{name: 'D', max: 38000},
{name: 'E', max: 52000} ]
},
series: [{ type: 'radar',
data : [ { value : [4300, 10000, 28000, 35000, 50000],
name : 'Data' } ] }]
};
barChart.setOption(barOption);
barHorizontalChart.setOption(barHorizontalOption);
lineChart.setOption(lineOption);
pieChart.setOption(pieOption);
radarChart.setOption(radarOption);
</script>
</body>
</html>
import matplotlib.pyplot as plt
# 定义 x 轴数据
x = [0, 1, 2, 3, 4]
# 定义 y 轴数据
y = [0, 1, 2, 3, 4]
# 绘制图形
plt.plot(x, y)
# 显示图形
plt.show()
import matplotlib.pyplot as plt
# x 轴数据
x = [1, 2, 3, 4, 5]
# y 轴数据
y = [2, 4, 6, 8, 10]
# 绘制折线图
plt.plot(x, y)
# 添加图表标题和坐标轴标签
plt.title('折线图示例')
plt.xlabel('X 轴')
plt.ylabel('Y 轴')
# 显示图形
plt.show()
import matplotlib.pyplot as plt
# x 轴数据
x = [1, 2, 3, 4, 5]
# y 轴数据
y = [2, 4, 6, 8, 10]
# 绘制折线图并设置线条样式为虚线
plt.plot(x, y, linestyle='--')
# 添加图表标题和坐标轴标签
plt.title('折线图示例')
plt.xlabel('X 轴')
plt.ylabel('Y 轴')
# 显示图形
plt.show()
import matplotlib.pyplot as plt
# x 轴数据
x = [1, 2, 3, 4, 5]
# y 轴数据
y = [2, 4, 6, 8, 10]
# 设置 x 轴刻度
plt.xticks([1, 2, 3, 4, 5], ['A', 'B', 'C', 'D', 'E'])
# 绘制折线图
plt.plot(x, y)
# 添加图表标题和坐标轴标签
plt.title('折线图示例')
plt.xlabel('自定义 X 轴')
plt.ylabel('Y 轴')
# 显示图形
plt.show()
import matplotlib.pyplot as plt
# x 轴数据
x = [1, 2, 3, 4, 5]
# y 轴数据
y = [2, 4, 6, 8, 10]
# 设置 x 轴字体大小和颜色
plt.xticks(fontsize=16, color='red')
# 设置 y 轴字体大小和颜色
plt.yticks(fontsize=16, color='red')
# 绘制折线图
plt.plot(x, y)
# 添加图表标题和坐标轴标签
plt.title('折线图示例')
plt.xlabel('X 轴')
plt.ylabel('Y 轴')
# 显示图形
plt.show()
import matplotlib.pyplot as plt
# x 轴数据
x = [1, 2, 3, 4, 5]
# y1 轴数据
y1 = [2, 4, 6, 8, 10]
# y2 轴数据
y2 = [4, 6, 8, 10, 12]
# 绘制第一个折线图
plt.subplot(211)
plt.plot(x, y1)
plt.title('折线图 1')
# 绘制第二个折线图
plt.subplot(212)
plt.plot(x, y2)
plt.title('折线图 2')
# 设置子图之间的间距
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.5, hspace=0.5)
# 显示图形
plt.show()
import matplotlib.pyplot as plt
import numpy as np
# 生成随机数据
x = np.arange(1, 11)
print(x)
y_line = np.random.randint(1, 10, size=10)
print(y_line)
y_bar = np.random.randint(1, 10, size=10)
y_barh = np.random.randint(1, 10, size=10)
theta = np.linspace(0, 2*np.pi, 10)
y_radar = np.random.randint(1, 10, size=10)
print(y_bar,y_barh,y_radar)
# 折线图
plt.figure(figsize=(8, 6)) #创建图像窗口,大小为8*6英寸
plt.plot(x, y_line, marker='o', linestyle='-', color='b') #绘制折线图,圆形标记,实线,蓝色
plt.title('折线图')
plt.xlabel('X轴')
plt.ylabel('Y轴')
plt.grid(True) #显示网格线
plt.show()
#柱状图
# 创建一个图像窗口,并设置大小为8x6英寸
plt.figure(figsize=(8, 6))
# 绘制柱状图,x为横坐标数据,y_bar为纵坐标数据,颜色为绿色,透明度为0.7
plt.bar(x, y_bar, color='g', alpha=0.7)
# 设置图像标题
plt.title('柱状图')
# 设置横坐标标签
plt.xlabel('X轴')
# 设置纵坐标标签
plt.ylabel('Y轴')
# 启用纵向网格线
plt.grid(axis='y')
# 显示图像
plt.show()
#条形图
# 创建一个图像窗口,并设置大小为8x6英寸
plt.figure(figsize=(8, 6))
# 绘制条形图,x为纵坐标数据,y_barh为横坐标数据,颜色为红色,透明度为0.7
plt.barh(x, y_barh, color='r', alpha=0.7)
# 设置图像标题
plt.title('条形图')
# 设置横坐标标签
plt.xlabel('X轴')
# 设置纵坐标标签
plt.ylabel('Y轴')
# 启用横向网格线
plt.grid(axis='x')
# 显示图像
plt.show()
#雷达图
# 创建一个图像窗口,并设置大小为8x6英寸
plt.figure(figsize=(8, 6))
# 创建一个极坐标子图
ax = plt.subplot(111, polar=True)
# 绘制雷达图,theta为角度数据,y_radar为雷达图的值,使用圆形标记
ax.plot(theta, y_radar, marker='o')
# 填充雷达图内部,颜色为蓝色,透明度为0.1
ax.fill(theta, y_radar, 'b', alpha=0.1)
# 设置图像标题
plt.title('雷达图')
# 显示图像
plt.show()
import matplotlib.pyplot as plt
# 数据
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
sizes = [20, 50, 80, 150, 300]
# 气泡大小
# 绘制气泡图
plt.scatter(x, y, s=sizes, alpha=0.6, c='blue', label='气泡')
# 添加标签和标题
plt.xlabel('X 轴')
plt.ylabel('Y 轴')
plt.title('简单的气泡图')
# 显示图例
plt.legend()
# 显示图表
plt.show()
import matplotlib.pyplot as plt
import numpy as np
# 定义数据
categories = ['类别1', '类别2', '类别3']
group1 = [3, 5, 2]
group2 = [4, 2, 7]
group3 = [6, 8, 3]
# 定义x轴的索引位置
x = np.arange(len(categories))
# 创建堆积柱形图
plt.bar(x, group1, label='组1')
plt.bar(x, group2, bottom=group1, label='组2')
plt.bar(x, group3, bottom=np.array(group1) + np.array(group2), label='组3')
# 添加x轴的标签
plt.xticks(x, categories)
# 添加图例
plt.legend()
# 添加标题和轴标签
plt.title('堆积柱形图')
plt.xlabel('类别')
plt.ylabel('数量')
# 显示图表
plt.show()
import matplotlib.pyplot as plt
# 数据
x = [20,21,22,23]
y1 = [10, 20, 15, 25]
y2 = [15, 25, 20, 30]
# 绘制簇状柱形图
width = 0.35
plt.bar(x, y1, width, label='Group 1')
plt.bar([i + 0.35 for i in x], y2, width, label='Group 2')
plt.xlabel('X 轴')
plt.ylabel('Y 轴')
plt.title('簇状柱形图')
plt.xticks(x)
plt.legend()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
# 定义数据
years = np.array([2016, 2017, 2018, 2019, 2020])
category1 = np.array([3, 4, 6, 7, 9])
category2 = np.array([1, 3, 4, 3, 6])
category3 = np.array([2, 2, 3, 4, 5])
# 绘制堆积折线图
plt.stackplot(years, category1, category2, category3,
labels=['类别1', '类别2', '类别3'],
colors=['#FFDDC1', '#FFABAB', '#FF6F61'])
# 添加图例
plt.legend(loc='upper left')
# 添加标题和轴标签
plt.title('堆积折线图示例')
plt.xlabel('年份')
plt.ylabel('数量')
# 显示图表
plt.show()