可视化学习(一)


一、折线图
情况一:同一坐标轴
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'SimSun' #不加这句无法显示中文
x = [5,7,11,17,19,25]#点的横坐标
k1 = [0.8222,0.918,0.9344,0.9262,0.9371,0.9353]#线1的纵坐标
k2 = [0.8988,0.9334,0.9435,0.9407,0.9453,0.9453]#线2的纵坐标
plt.plot(x,k1,'v-',color = 'r',label="例1")#s-:方形
plt.plot(x,k2,'o:',color = 'g',label="例2")#o-:圆形
'''
-------------------------------------------------------------
颜色字符
'b' 蓝色 'm' 洋红色 magenta
'g' 绿色 'y' 黄色
'r' 红色 'k' 黑色
'w' 白色 'c' 青绿色 cyan
'#008000' RGB某颜色 '0.8' 灰度值字符串
--------------------------------------------------------------
标记字符
'.' 点标记
',' 像素标记(极小点)
'o' 实心圈标记
'v' 倒三角标记
's' 正方形标记
'^' 上三角标记
'>' 右三角标记
'<' 左三角标记
-------------------------------------------------------------
风格字符
'‐' 实线
'‐‐' 破折线
'‐.' 点划线
':' 虚线
'' ' ' 无线条
--------------------------------------------------------------
'''
plt.xlabel("x坐标")#横坐标名字
plt.ylabel("y坐标")#纵坐标名字
plt.legend(loc = "upper center")#图例
'''
---------------------------------------------------------
loc为图例添加位置
best
upper right
upper left
lower left
lower right
right
center left
center right
lower center
upper center
center
------------------------------------------------------------
'''
plt.show()


情况二:不同坐标轴

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = 'SimSun' #不加这句无法显示中文

x = ['a组', 'b组', 'c组']
y1 = [30, 10, 50]
y2 = [0.2,0.4,0.6]
y3 = [45,65,36]
plt.figure(figsize=(6, 6)) #图形大小,则每个图形3*3

plt.subplot(221) #1*3形式的第1个图
plt.plot(x, y1)
plt.subplot(222) #1*3形式的第2个图
plt.plot(x, y2)
plt.subplot(223) #1*3形式的第3个图
plt.plot(x, y3)

plt.suptitle('xx问题研究')
plt.show()

 二、柱形图

情况一:同一坐标轴

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = 'SimSun' #不加这句无法显示中文
#柱状图
ATT_LSTM = [0.8892,0.861,0.9243]
MATT_CNN = [0.8966,0.8556,0.9316]
ATT_RLSTM = [0.8867,0.8543,0.9344]
CNN_RLSTM = [0.9016,0.8636,0.9435]
xl = ['指标一','指标二','指标三']
x = np.arange(3) #总共有几组,就设置成几,我们这里有三组,所以设置为3
total_width, n = 0.8, 4 # 有多少个类型,只需更改n即可,比如这里我们对比了四个,那么就把n设成4
width = total_width / n
x = x - (total_width - width) / 2 #设置每一组横坐标的间距
#不让每个柱形图重叠,横坐标错开
plt.bar(x, ATT_LSTM, color = "r",width=width,label='方法一')
plt.bar(x + width, MATT_CNN, color = "y",width=width,label='方法二')
plt.bar(x + 2 * width, ATT_RLSTM , color = "c",width=width,label='方法三')
plt.bar(x + 3 * width, CNN_RLSTM , color = "g",width=width,label='方法四')
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.legend(loc = "best")
plt.xticks(x,xl) #将横坐标x的标签用xl替换
my_y_ticks = np.arange(0.8, 0.95, 0.02) #设置纵坐标起始点和间距
plt.ylim((0.8, 0.95)) #纵坐标的范围
plt.yticks(my_y_ticks)
plt.title('xxx问题研究')
plt.show()

情况二:不同坐标轴

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = 'SimSun' #不加这句无法显示中文

import matplotlib.pyplot as plt
import numpy as np

x = ['a组', 'b组', 'c组']
y1 = [30, 10, 50]
y2 = [0.2,0.4,0.6]
y3 = [45,65,36]
plt.figure(figsize=(9, 3))

plt.subplot(131) #1*3形式的第1个图
plt.bar(x, y1)
plt.subplot(132) #1*3形式的第2个图
plt.bar(x, y2)
plt.subplot(133) #1*3形式的第3个图
plt.bar(x, y3)

plt.suptitle('xx问题研究')
plt.show()

三、柱形图+折线图:双y轴

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = 'SimSun' #不加这句无法显示中文

fig, ax1 = plt.subplots() # 使用subplots()创建窗口
#fig = plt.figure(figsize=(20,8),dpi=80)
#ax1 = fig.add_subplot(111)
xl =['指标1','指标2','指标三','指标四']
x = [1,2,3,4]
y1 = [34,46,67,28]
y2 = [0.6,0.4,0.7,0.5]
ax2 = ax1.twinx() # 创建第二个坐标轴

lin1 = ax1.bar(x, y1,color = 'b',label = '数量',linewidth = 2)
lin2 = ax2.plot(x, y2,color = 'g',label = '增长率',linewidth = 2)

ax1.set_xlabel('指标', fontsize = 12) # fontsize使用方法和plt.xlabel()中一样
ax1.set_ylabel('数量', fontsize = 12)
ax2.set_ylabel('增长率', fontsize = 12)

#ax1.set_xticklabels(x,rotation=45)#小技巧:可以让x标签旋转45度

ax1.legend(loc=0)
ax2.legend(loc=4)
'''
-------------------------------------------------------------
'best'         : 0
     |    'upper right'  
     |    'upper left'   : 2,
     |    'lower left'   : 3,
     |    'lower right'  : 4,
     |    'right'        : 5,
     |    'center left'  : 6,
     |    'center right' : 7,
     |    'lower center' : 8,
     |    'upper center' : 9,
     |    'center'       : 10,
-----------------------------------------------------------
'''
ax1.set_xlim([0, max(x)]) # 设置坐标轴范围的语句有所变化
ax1.set_ylim(min(y1), max(y1))
ax2.set_ylim([min(y2), max(y2)])
plt.title('xx问题研究')
plt.xticks(x,xl)
#plt.legend()
plt.show()

 参考:

https://blog.csdn.net/TeFuirnever/article/details/99672296

https://akastan.blog.csdn.net/article/details/121883574

 

posted @   萧六弟  阅读(80)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示