【问题标题】:Two Y axis Bar plot: custom xticks两个 Y 轴条形图:自定义 xticks
【发布时间】:2016-10-13 00:38:55
【问题描述】:

我正在尝试将自定义 xticks 添加到相对复杂的条形图中,但我被卡住了。

我正在从两个数据框绘制,merged_90merged_15

merged_15
                 Volume     y_err_x      Area_2D     y_err_y
TripDate                                                    
2015-09-22  1663.016032  199.507503  1581.591701  163.473202

merged_90

                 Volume     y_err_x      Area_2D     y_err_y
TripDate                                                    
1990-06-10  1096.530711  197.377497  1531.651913  205.197493

我想创建一个带有两个轴(即Area_2DVolume)的条形图,其中Area_2DVolume 条基于它们各自的数据框进行分组。示例脚本如下所示:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy


fig = plt.figure()
ax1 = fig.add_subplot(111)
merged_90.Volume.plot(ax=ax1, color='orange', kind='bar',position=2.5, yerr=merged_90['y_err_x'] ,use_index=False , width=0.1)
merged_15.Volume.plot(ax=ax1, color='red', kind='bar',position=0.9, yerr=merged_15['y_err_x'] ,use_index=False, width=0.1)


ax2 = ax1.twinx()
merged_90.Area_2D.plot(ax=ax2,color='green',  kind='bar',position=3.5, yerr=merged_90['y_err_y'],use_index=False, width=0.1)
merged_15.Area_2D.plot(ax=ax2,color='blue',  kind='bar',position=0, yerr=merged_15['y_err_y'],use_index=False, width=0.1)

ax1.set_xlim(-0.5,0.2)

x = scipy.arange(1)
ax2.set_xticks(x)
ax2.set_xticklabels(['2015'])
plt.tight_layout()
plt.show()

结果图是:

有人会认为我可以改变:

x = scipy.arange(1)
ax2.set_xticks(x)
ax2.set_xticklabels(['2015'])

x = scipy.arange(2)
ax2.set_xticks(x)
ax2.set_xticklabels(['1990','2015'])

但这会导致:

我想查看按时间顺序排列的刻度(即 1990,2015)

谢谢!

【问题讨论】:

    标签: python pandas matplotlib bar-chart


    【解决方案1】:

    您是否考虑过删除第二个轴并将它们绘制如下:

    ind = np.array([0,0.3])
    width = 0.1
    fig, ax = plt.subplots()
    Rects1 = ax.bar(ind, [merged_90.Volume.values, merged_15.Volume.values], color=['orange', 'red'] ,width=width)
    Rects2 = ax.bar(ind + width, [merged_90.Area_2D.values, merged_15.Area_2D.values], color=['green', 'blue'] ,width=width)
    ax.set_xticks([.1,.4]) 
    ax.set_xticklabels(('1990','2015'))
    

    这会产生:

    我省略了错误和颜色,但您可以轻松添加它们。给定您的测试数据,这将产生一个可读的图表。正如您在 cmets 中提到的,您仍然希望有两个轴,大概是针对具有适当比例的不同数据。为此,您可以这样做: 无花果 = plt.figure() ax1 = fig.add_subplot(111) merge_90.Volume.plot(ax=ax, color='orange', kind='bar',position=2.5, use_index=False, width=0.1) merge_15.Volume.plot(ax=ax, color='red', kind='bar',position=1.0, use_index=False, width=0.1) ax2 = ax1.twinx() merge_90.Area_2D.plot(ax=ax,color='green', kind='bar',position=3.5,use_index=False, width=0.1) merge_15.Area_2D.plot(ax=ax,color='blue', kind='bar',position=0,use_index=False, width=0.1) ax1.set_xlim([-.45, .2]) ax2.set_xlim(-.45, .2]) ax1.set_xticks([-.35, 0]) ax1.set_xticklabels([1990, 2015])

    这会产生:

    您的问题是仅重置一个轴限制而不是另一个轴限制,它们被创建为双胞胎,但不一定遵循对彼此所做的更改。

    【讨论】:

    • 为什么要删除第二个轴?我需要两个 Y 轴。
    • 另外,您需要先将 pandas 系列转换为 NDarray,然后才能进行绘图(例如 merged_90.Volume.values
    • 我确定您还有其他数据,但您提供的数据实际上导致了一个非常难以解释的图表。由于天平如此接近,因此很难传播实际发生的事情。
    • 你的意思是VolumeArea_2D 的大小相似?我的帖子标题是Two Y axis Bar plot: custom xticks
    • 太棒了!非常感谢。您可能希望对代码进行样式化,以便更易于阅读,但您忘记为体积图设置 ax=ax1 为 Area_2D 图设置 ax = ax2。此外,您在ax2.set_xlim(-.45, .2]) 中缺少一个左括号
    猜你喜欢
    • 2014-08-02
    • 2019-06-01
    • 2019-01-23
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多