【问题标题】:Problem in combining bar plot and line plot (python)组合条形图和线图的问题(python)
【发布时间】:2020-06-22 03:17:24
【问题描述】:

我有这样的数据框:

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值是完全一样的值。

我的期望是将这 2 个图合并为 1 个图:

这是我制作的代码:

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()

有人可以帮帮我吗?

【问题讨论】:

    标签: python pandas numpy matplotlib bar-chart


    【解决方案1】:

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

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

    这样情节使用 seaborn 进行:

    【讨论】:

      【解决方案2】:

      Seaborn 和 pandas 对条形图使用分类 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()
      

      【讨论】:

        猜你喜欢
        • 2013-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多