Matplotlib 中的线图样式
Python是一种高级、解释性和动态类型的编程语言,可用于管理庞大的数据集。 Python支持多种数据可视化库,如 Matplotlib、Seaborn、Bokeh、Geoplotlib、Ggplot 和 Plotly。在所有这些库中,Matplotlib 相对简单,易于实现。 Python的 Matplotlib 库是数据可视化的流行选择,因为它具有广泛的图表类型及其可用于创建图表样式的属性。 matplotlib.pyplot.plot(*args, **kwargs) 方法用于绘制图形并指定图形样式,如颜色或线条样式。
Matplotlib 中提供了以下线条样式:
Following line styles are available in Matplotlib | |
---|---|
Character | Definition |
– | Solid line |
— | Dashed line |
-. | dash-dot line |
: | Dotted line |
. | Point marker |
o | Circle marker |
, | Pixel marker |
v | triangle_down marker |
^ | triangle_up marker |
< | triangle_left marker |
> | triangle_right marker |
1 | tri_down marker |
2 | tri_up marker |
3 | tri_left marker |
4 | tri_right marker |
s | square marker |
p | pentagon marker |
* | star marker |
h | hexagon1 marker |
H | hexagon2 marker |
+ | Plus marker |
x | X marker |
D | Diamond marker |
d | thin_diamond marker |
| | vline marker |
_ | hline marker |
可与线条样式一起使用的颜色代码缩写: Codes Description b blue g green r red c cyan m magenta y yellow k black w white
以下示例演示了绘制具有不同线条样式的图形:
示例 1:
在本例中,导入了 matplotlib.pyplot 库。学生姓名被添加到学生列表中,分数列表是在 random.randint() 方法中创建的。接下来,标记 X 轴和 Y 轴,并为图形指定标题。最后,使用 matplotlib.pyplot 的 plot() 方法绘制图形。这里使用了颜色和线条样式的缩写形式。选择的颜色缩写是“m”,它是洋红色,而选择的线条样式是“-”,它是虚线样式。
下面是实现:
Python3
import matplotlib.pyplot as plt
import random as random
students = ["Jane","Joe","Beck","Tom",
"Sam","Eva","Samuel","Jack",
"Dana","Ester","Carla","Steve",
"Fallon","Liam","Culhane","Candance",
"Ana","Mari","Steffi","Adam"]
marks=[]
for i in range(0,len(students)):
marks.append(random.randint(0, 101))
plt.xlabel("Students")
plt.ylabel("Marks")
plt.title("CLASS RECORDS")
plt.plot(students,marks,'m--')
Python3
import matplotlib.pyplot as plt
import random as random
students = ["Jane","Joe","Beck","Tom","Sam",
"Eva","Samuel","Jack","Dana","Ester",
"Carla","Steve","Fallon","Liam","Culhane",
"Candance","Ana","Mari","Steffi","Adam"]
marks=[]
for i in range(0,len(students)):
marks.append(random.randint(0, 101))
plt.xlabel("Students")
plt.ylabel("Marks")
plt.title("CLASS RECORDS")
plt.plot(students, marks, color = 'green',
linestyle = 'solid', marker = 'o',
markerfacecolor = 'red', markersize = 12)
输出:
示例 2:
在本例中,导入了 matplotlib.pyplot 库。学生姓名被添加到学生列表中,分数列表由 random.randint() 方法创建。接下来,标记 X 轴和 Y 轴,并为图形指定标题。最后,使用 matplotlib.pyplot 的 plot() 方法绘制图形。这里不是使用缩写格式,而是使用关键字参数控制行属性。为关键字分配了所需的值。使用的颜色是绿色,线型是实心的,标记是圆形标记,标记颜色是红色,标记大小是 12px。
下面是实现:
蟒蛇3
import matplotlib.pyplot as plt
import random as random
students = ["Jane","Joe","Beck","Tom","Sam",
"Eva","Samuel","Jack","Dana","Ester",
"Carla","Steve","Fallon","Liam","Culhane",
"Candance","Ana","Mari","Steffi","Adam"]
marks=[]
for i in range(0,len(students)):
marks.append(random.randint(0, 101))
plt.xlabel("Students")
plt.ylabel("Marks")
plt.title("CLASS RECORDS")
plt.plot(students, marks, color = 'green',
linestyle = 'solid', marker = 'o',
markerfacecolor = 'red', markersize = 12)
输出: