栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

matplotlib 散点图和折线图画在一起_matplotlib双轴图?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

matplotlib 散点图和折线图画在一起_matplotlib双轴图?

文章目录

1. Matplotlib多 y 轴折线图--科研美图2. 调整字体大小

1. Matplotlib多 y 轴折线图–科研美图
from mpl_toolkits.axisartist.parasite_axes import HostAxes, ParasiteAxes
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决汉字显示为□指定默认字体为黑体。
plt.rcParams['axes.unicode_minus'] = False   # 解决保存图像时 负号'-' 显示为□和报错的问题。
data=pd.read_csv(r'C:UsersmengxDesktop机器学习岩石力学参数预测部分all_ma_log.csv',encoding='gb2312',engine='python')
print(data.columns)


fig = plt.figure(1,figsize=(10,4)) #定义figure
ax_1 = HostAxes(fig, [0, 0, 0.9, 0.9])  #用[left, bottom, weight, height]的方式定义axes,0 <= l,b,w,h <= 1

#parasite addtional axes, share x
ax_2 = ParasiteAxes(ax_1, sharex=ax_1)
ax_3 = ParasiteAxes(ax_1, sharex=ax_1)
ax_4 = ParasiteAxes(ax_1, sharex=ax_1)
ax_5 = ParasiteAxes(ax_1, sharex=ax_1)
ax_6 = ParasiteAxes(ax_1, sharex=ax_1)


#append axes
ax_1.parasites.append(ax_2)
ax_1.parasites.append(ax_3)
ax_1.parasites.append(ax_4)
ax_1.parasites.append(ax_5)
ax_1.parasites.append(ax_6)

#invisible right axis of ax_1
ax_1.axis['right'].set_visible(False)
ax_1.axis['top'].set_visible(False)
ax_2.axis['right'].set_visible(True)
ax_2.axis['right'].major_ticklabels.set_visible(True)
ax_2.axis['right'].label.set_visible(True)

#set label for axis
ax_1.set_ylabel('cof')
ax_1.set_xlabel('Distance (m)')
ax_2.set_ylabel('Temperature')
ax_3.set_ylabel('load')
ax_4.set_ylabel('CP')
ax_5.set_ylabel('Wear')
ax_6.set_ylabel('Wear')

three_axisline = ax_3.get_grid_helper().new_fixed_axis
four_axisline = ax_4.get_grid_helper().new_fixed_axis
five_axisline = ax_5.get_grid_helper().new_fixed_axis
six_axisline = ax_6.get_grid_helper().new_fixed_axis

ax_3.axis['right2'] = three_axisline(loc='right', axes=ax_3, offset=(40,0))
ax_4.axis['right3'] = four_axisline(loc='right', axes=ax_4, offset=(80,0))
ax_5.axis['right4'] = five_axisline(loc='right', axes=ax_5, offset=(120,0))
ax_6.axis['right5'] = six_axisline(loc='right', axes=ax_6, offset=(160,0))

fig.add_axes(ax_1)

''' #set limit of x, y
ax_1.set_xlim(0,2)
ax_1.set_ylim(0,3)
'''

curve_1, = ax_1.plot(range(data['抗压强度'].shape[0]),data['抗压强度'], label="抗压强度", color='black')
curve_2, = ax_2.plot(range(data['抗拉强度'].shape[0]),data['抗拉强度'], label="抗拉强度", color='red')
curve_3, = ax_3.plot(range(data['内聚力'].shape[0]),data['内聚力'], label="内聚力", color='green')
curve_4, = ax_4.plot(range(data['内摩擦角'].shape[0]),data['内摩擦角'], label="内摩擦角", color='pink')
curve_5, = ax_5.plot(range(data['弹性模量'].shape[0]),data['弹性模量'], label="弹性模量", color='blue')
curve_6, = ax_6.plot(range(data['泊松比'].shape[0]),data['泊松比'], label="泊松比", color='c')

ax_2.set_ylim(data['抗拉强度'].min()-0.3,data['抗拉强度'].max()+0.3)
ax_3.set_ylim(data['内聚力'].min()-0.3,data['内聚力'].max()+0.3)
ax_4.set_ylim(data['内摩擦角'].min()-0.3,data['内摩擦角'].max()+0.3)
ax_5.set_ylim(data['弹性模量'].min()-0.3,data['弹性模量'].max()+0.3)
ax_6.set_ylim(data['泊松比'].min()-0.3,data['泊松比'].max()+0.3)

ax_1.legend()

#轴名称,刻度值的颜色
#ax_1.axis['left'].label.set_color(ax_1.get_color())
ax_2.axis['right'].label.set_color('red')
ax_3.axis['right2'].label.set_color('green')
ax_4.axis['right3'].label.set_color('pink')
ax_5.axis['right4'].label.set_color('blue')
ax_6.axis['right5'].label.set_color('c')

ax_2.axis['right'].major_ticks.set_color('red')
ax_3.axis['right2'].major_ticks.set_color('green')
ax_4.axis['right3'].major_ticks.set_color('pink')
ax_5.axis['right4'].major_ticks.set_color('blue')
ax_6.axis['right5'].major_ticks.set_color('c')

ax_2.axis['right'].major_ticklabels.set_color('red')
ax_3.axis['right2'].major_ticklabels.set_color('green')
ax_4.axis['right3'].major_ticklabels.set_color('pink')
ax_5.axis['right4'].major_ticklabels.set_color('blue')
ax_6.axis['right5'].major_ticklabels.set_color('c')

ax_2.axis['right'].line.set_color('red')
ax_3.axis['right2'].line.set_color('green')
ax_4.axis['right3'].line.set_color('pink')
ax_5.axis['right4'].line.set_color('blue')
ax_6.axis['right5'].line.set_color('c')

plt.show()

2. 调整字体大小
params = {'axes.labelsize': 20, 'axes.titlesize':20, 'legend.fontsize': 20, 'xtick.labelsize': 20, 'ytick.labelsize': 20}
plt.rcParams.update(params)
from mpl_toolkits.axisartist.parasite_axes import HostAxes, ParasiteAxes
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决汉字显示为□指定默认字体为黑体。
plt.rcParams['axes.unicode_minus'] = False   # 解决保存图像时 负号'-' 显示为□和报错的问题。
plt.rcParams['font.size'] = 14
data=pd.read_csv(r'C:UsersmengxDesktop机器学习岩石力学参数预测部分all_ma_log.csv',encoding='gb2312',engine='python')
print(data.columns)
fig = plt.figure(1,figsize=(8,4)) #定义figure
ax_1 = HostAxes(fig, [0, 0, 0.9, 0.9])  #用[left, bottom, weight, height]的方式定义axes,0 <= l,b,w,h <= 1
#parasite addtional axes, share x
ax_2 = ParasiteAxes(ax_1, sharex=ax_1)
ax_3 = ParasiteAxes(ax_1, sharex=ax_1)
ax_4 = ParasiteAxes(ax_1, sharex=ax_1)
#append axes
ax_1.parasites.append(ax_2)
ax_1.parasites.append(ax_3)
ax_1.parasites.append(ax_4)
#invisible right axis of ax_1
ax_1.axis['right'].set_visible(False)
ax_1.axis['top'].set_visible(False)
ax_2.axis['right'].set_visible(True)
ax_2.axis['right'].major_ticklabels.set_visible(True)
ax_2.axis['right'].label.set_visible(True)
#set label for axis
ax_1.set_xlabel('样本点数')
ax_1.set_ylabel('视电阻率')
ax_2.set_ylabel('自然伽玛')
ax_3.set_ylabel('双收时差')
ax_4.set_ylabel('抗压强度')

three_axisline = ax_3.get_grid_helper().new_fixed_axis
four_axisline = ax_4.get_grid_helper().new_fixed_axis

ax_3.axis['right2'] = three_axisline(loc='right', axes=ax_3, offset=(60,0))
ax_4.axis['right3'] = four_axisline(loc='right', axes=ax_4, offset=(120,0))

fig.add_axes(ax_1)

''' #set limit of x, y
ax_1.set_xlim(0,2)
ax_1.set_ylim(0,3)
'''
yyy=200*(data['自然伽玛']-data['自然伽玛'].min())/(data['自然伽玛'].max()-data['自然伽玛'].min())
# curve_1, = ax_1.plot(range(data['视电阻率'].shape[0]),data['视电阻率'], label="视电阻率", color='black',linestyle=':')
curve_2, = ax_2.plot(range(data['自然伽玛'].shape[0])
                     ,yyy
                     ,label="自然伽玛", color='red')
# curve_3, = ax_3.plot(range(data['双收时差'].shape[0]),data['双收时差'], label="双收时差", color='green')
curve_4, = ax_4.plot(range(data['抗压强度'].shape[0]),data['抗压强度'], label="抗压强度", color='lime',linestyle='--')

ax_2.set_ylim(yyy.min()-20,yyy.max()+20)
ax_3.set_ylim(data['双收时差'].min()-300,data['双收时差'].max()+600)
ax_4.set_ylim(data['抗压强度'].min()-10,data['抗压强度'].max()+10)


# ax_4.set_yticks(fontproperties = 'Times New Roman')
# ax_4.get_yticklabels().set_fontname('Times New Roman')

ax_1.legend(fontsize=12)

#设置坐标轴,刻度值的颜色
#ax_1.axis['left'].label.set_color(ax_1.get_color())
ax_2.axis['right'].label.set_color('red')
ax_3.axis['right2'].label.set_color('green')
ax_4.axis['right3'].label.set_color('lime')

ax_2.axis['right'].major_ticks.set_color('red')
ax_3.axis['right2'].major_ticks.set_color('green')
ax_4.axis['right3'].major_ticks.set_color('lime')

ax_2.axis['right'].major_ticklabels.set_color('red')
ax_3.axis['right2'].major_ticklabels.set_color('green')
ax_4.axis['right3'].major_ticklabels.set_color('lime')

ax_2.axis['right'].line.set_color('red')
ax_3.axis['right2'].line.set_color('green')
ax_4.axis['right3'].line.set_color('lime')
plt.show()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783280.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号