2 回答

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
Seaborn 和 pandas 對(duì)條形圖(內(nèi)部編號(hào)為 0,1,2,...)使用分類 x 軸,對(duì)線圖使用浮點(diǎn)數(shù)。請(qǐng)注意,您的 x 值不是均勻分布的,因此條形圖之間會(huì)有奇怪的距離,或者不會(huì)與線圖中的 x 值對(duì)齊。
下面是一個(gè)使用標(biāo)準(zhǔn) matplotlib 來組合兩個(gè)圖形的解決方案。
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()

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
添加回答
舉報(bào)