# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@FileName: mstplot元素
@Time: 2022/1/6,15:52
@Motto: 每天都要奥力给
"""
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv(r'E:\numpy-pandas\mapping.csv')
print(df.head())
"""
Unnamed: 0 Time variable value
0 1 -0.049505 0%(Control) 0.000000
1 2 0.891089 0%(Control) 0.803213
2 3 1.881188 0%(Control) 0.803213
3 4 3.019802 0%(Control) 0.803213
4 5 4.108911 0%(Control) 0.401606
"""
df1 = df[df.variable == '0%(Control)']
df2 = df[df.variable == '1%']
df3 = df[df.variable == '5%']
df4 = df[df.variable == '15%']
# =============================================================================
colors = ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3']
markers = ['v', 'o', 'H', 'D'] # 设置点的形状
labels = ["0%(Control)", "1%", "5%", "15%"] # 图例上字 即 labels
group = ["0%(Control)", "1%", "5%", "15%"] # np.unique(df.variable)
# ----------------------------(a)-----------------------------------------------
fig = plt.figure(figsize=(4, 3), dpi=100)
for i in range(0, 4):
temp_df = df[df.variable == group[i]]
plt.plot(temp_df.Time, temp_df.value)
# plt.plot(df2.Time, df2.value)
# plt.plot(df3.Time, df3.value)
# plt.plot(df4.Time, df4.value)
plt.show() #图形一
# fig.savefig("matplotlib1.pdf")
# -------------------------------(b)--------------------------------------------
fig = plt.figure(figsize=(4, 3), dpi=100)
for i in range(0, 4):
"""
使用循环用列表索引 0 1 2 3分别取值
"""
plt.plot(df[df.variable == group[i]].Time, df[df.variable == group[i]].value,
marker=markers[i], markerfacecolor=colors[i], markersize=5, markeredgewidth=0.05, # 设置marker 点的格式
color="k", linewidth=0.9, linestyle="-", label=group[i]) # 设置线的格式,包括颜色是黑色、线宽0.9、线形——、
# plt.legend(loc='upper left',edgecolor='none',facecolor='none')
plt.show() #图形二
# fig.savefig("matplotlib2.pdf")
# ---------------------------------(c)------------------------------------------
fig = plt.figure(figsize=(4, 3), dpi=100)
"""
下面是没用循环,一条一条的往 fig上画线
"""
plt.plot(df1.Time, df1.value,
marker=markers[0], markerfacecolor=colors[0], markersize=8, markeredgewidth=0.5,
color="k", linewidth=0.5, linestyle="-", label=labels[0])
plt.plot(df2.Time, df2.value,
marker=markers[1], markerfacecolor=colors[1], markersize=7, markeredgewidth=0.5,
color="k", linewidth=0.5, linestyle="-", label=labels[1])
plt.plot(df3.Time, df3.value,
marker=markers[2], markerfacecolor=colors[2], markersize=8, markeredgewidth=0.5,
color="k", linewidth=0.5, linestyle="-", label=labels[2])
plt.plot(df4.Time, df4.value,
marker=markers[3], markerfacecolor=colors[3], markersize=7, markeredgewidth=0.5,
color="k", linewidth=0.5, linestyle="-", label=labels[3])
plt.xlabel("Time(d)", fontsize=14) # 设置X轴 标题和字号大小
plt.ylabel("value", fontsize=14) # 设置y轴 标题和字号大小
plt.xlim(-1, 20) # X轴范围
plt.ylim(-2, 90) # y轴范围
# 设置横轴记号
plt.xticks(np.linspace(0, 20, 11, endpoint=True), fontsize=10) # X标签从0到20平均分为11 - 1份也就是10份。
# 设置纵轴的上下限
plt.yticks(np.linspace(0, 90, 10, endpoint=True), fontsize=10) # y标签从0到90平均分为10-1 9份,还有大小
ax = plt.gca() # plt.gca(),全称是get current axis
ax.spines['right'].set_color('r') # 将右边的axis设置为红色
ax.spines['top'].set_color('b') # 将上面的axis设置为绿色 ax.spines['right'].set_color('none'),取消右边 asix
plt.legend(loc='upper left', edgecolor='none', facecolor='none') # 添加图例,位置左上边、边框颜色无、中间填充颜色无
plt.show() # 图形三
# fig.savefig(r"F:\桌面\matplotlib3.pdf") 保存位置
Figure_1.png
Figure_2.png
Figure_3.png
《python数据可视化之美》 代码来自于作者张杰
网友评论