matplotlib 条形图和折线图组合时出现问题

osh3o9ms  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(150)

我有这样的 Dataframe :

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值是完全相同的值。
我的期望是将这两个图合并为一个图:

这是我编的代码

import numpy as np
import pandas as pd
import seaborn as sns
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)"]
T1= df_meshX_min_select["Different Result(Temperature)"]

#Create combo chart
fig, ax1 = plt.subplots(figsize=(10,6))
color = 'tab:green'
#bar plot creation
ax1.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-axis
ax2 = ax1.twinx()
color = 'tab:red'
#line plot creation
ax2.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 plot

plt.show()

有人能帮帮我吗?

wlzqhblo

wlzqhblo1#

Seaborn和panda使用分类x轴表示条形图(内部编号为0,1,2,...),使用浮点数表示线图。请注意,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()

lrpiutwd

lrpiutwd2#

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

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

使用seaborn进行绘图的方式如下:

相关问题