为了账号安全,请及时绑定邮箱和手机立即绑定

组合条形图和线图的问题(python)

组合条形图和线图的问题(python)

炎炎设计 2022-08-25 15:21:05
我有这样的数据帧:df_meshX_min_select = pd.DataFrame({'Number of Elements'  : [5674, 8810,13366,19751,36491],'Time (a)'            : [42.14, 51.14, 55.64, 55.14, 56.64],'Different Result(Temperature)' : [0.083849, 0.057309, 0.055333, 0.060516, 0.035343]})我试图在同一图中组合条形图(元素数与不同结果)和线图(元素数与时间),但我发现了以下问题:在组合 2 个图时,x_value似乎不匹配,但是如果您看到数据框,则 x 值完全相同。我的期望是将这两个图组合成1个数字: 这是我做的代码:import numpy as npimport pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltimport matplotlib.ticker as tickerdf_meshX_min_select = pd.DataFrame({    'Number of Elements'  : [5674, 8810,13366,19751,36491],    'Time (a)'            : [42.14, 51.14, 55.64, 55.14, 56.64],    'Different Result(Temperature)' : [0.083849, 0.057309, 0.055333, 0.060516, 0.035343]})x1= df_meshX_min_select["Number of Elements"]t1= df_meshX_min_select["Time (a)"]T1= df_meshX_min_select["Different Result(Temperature)"]#Create combo chartfig, ax1 = plt.subplots(figsize=(10,6))color = 'tab:green'#bar plot creationax1.set_title('Mesh Analysis', fontsize=16)ax1.set_xlabel('Number of elements', fontsize=16)ax1.set_ylabel('Different Result(Temperature)', fontsize=16)ax1 = sns.barplot(x='Number of Elements', y='Different Result(Temperature)', data = df_meshX_min_select)ax1.tick_params(axis='y')#specify we want to share the same x-axisax2 = ax1.twinx()color = 'tab:red'#line plot creationax2.set_ylabel('Time (a)', fontsize=16)ax2 = sns.lineplot(x='Number of Elements', y='Time (a)', data = df_meshX_min_select, sort=False, color=color, ax=ax2)ax2.tick_params(axis='y', color=color)#show plotplt.show()任何人都可以帮我,请?
查看完整描述

2 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

Seaborn 和 pandas 对条形图(内部编号为 0,1,2,...)使用分类 x 轴,对线图使用浮点数。请注意,您的 x 值不是均匀分布的,因此条形图之间会有奇怪的距离,或者不会与线图中的 x 值对齐。


下面是一个使用标准 matplotlib 来组合两个图形的解决方案。


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import matplotlib.ticker as ticker


df_meshx_min_select = pd.DataFrame({

    'number of elements': [5674, 8810, 13366, 19751, 36491],

    'time (a)': [42.14, 51.14, 55.64, 55.14, 56.64],

    'different result(temperature)': [0.083849, 0.057309, 0.055333, 0.060516, 0.035343]})

x1 = df_meshx_min_select["number of elements"]

t1 = df_meshx_min_select["time (a)"]

d1 = df_meshx_min_select["different result(temperature)"]


fig, ax1 = plt.subplots(figsize=(10, 6))

color = 'limegreen'

ax1.set_title('mesh analysis', fontsize=16)

ax1.set_xlabel('number of elements', fontsize=16)

ax1.set_ylabel('different result(temperature)', fontsize=16, color=color)

ax1.bar(x1, height=d1, width=2000, color=color)

ax1.tick_params(axis='y', colors=color)


ax2 = ax1.twinx()  # share the x-axis, new y-axis

color = 'crimson'

ax2.set_ylabel('time (a)', fontsize=16, color=color)

ax2.plot(x1, t1, color=color)

ax2.tick_params(axis='y', colors=color)


plt.show()

在此输入图像描述

查看完整回答
反对 回复 2022-08-25
?
红糖糍粑

TA贡献1815条经验 获得超6个赞

我正在用线图绘制一个箱线图,即使我的两个x轴相同,我也遇到了同样的问题,所以我解决了将x轴特征转换为类型字符串的问题:

df_meshX_min_select['Number of Elements'] = df_meshX_min_select['Number of Elements'].astype('string')

通过这种方式,情节使用seaborn工作:

在此输入图像描述


查看完整回答
反对 回复 2022-08-25
  • 2 回答
  • 0 关注
  • 145 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号