简体   繁体   English

如何将条形图标签移动到 Y 轴?

[英]How to move bar chart labels to the Y-axis?

I want to have the bar labels marked on the Y-axis, instead of top of the bar charts.我想在 Y 轴上标记条形标签,而不是条形图的顶部。 Is there a way to do this?有没有办法做到这一点?

在此处输入图像描述

I have quite a lengthy piece of code to recreate this plot, so reproducing only part of it.我有相当长的一段代码来重新创建这个 plot,所以只复制它的一部分。 My only idea is to do it one by one through ax.patches..我唯一的想法是通过 ax.patches 一个一个来做。

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

np.random.seed(12345)

df = pd.DataFrame([np.random.normal(32000,200000,3650), 
                   np.random.normal(43000,100000,3650), 
                   np.random.normal(43500,140000,3650), 
                   np.random.normal(48000,70000,3650)], 
                  index=[1992,1993,1994,1995])
df
....
bar_plot = plt.bar(df.index, df.mean(axis=1),yerr=upper, edgecolor='indigo', color=color)  
for i in ax.patches:

    ax.text(i.get_x()+0.2, i.get_height()-5.8, \
            str(round((i.get_height()), 1)), fontsize=14, color='darkblue')

To also show the heights on the y-axis, one could introduce minor y-ticks at those positions.为了也显示 y 轴上的高度,可以在这些位置引入较小的 y 刻度。 Optionally, gridlines can be drawn there.或者,可以在此处绘制网格线。

For the minor y tick labels not to interfere with the major y tick labels, a possibility is to make the ticks larger which moves the ticks to the left.对于不干扰主要 y 刻度标签的次要 y 刻度标签,一种可能性是使刻度更大,从而将刻度向左移动。

Other possibilities would be to remove the major ticks entirely ( plt.yticks([]) ), or to draw either of the ticks at the right side.其他可能性是完全删除主要刻度( plt.yticks([]) ),或在右侧绘制任一刻度。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FixedLocator, FormatStrFormatter

np.random.seed(12345)

df = pd.DataFrame([np.random.normal(32000, 200000, 3650),
                   np.random.normal(43000, 100000, 3650),
                   np.random.normal(43500, 140000, 3650),
                   np.random.normal(48000, 70000, 3650)],
                  index=[1992, 1993, 1994, 1995])
means = df.mean(axis=1)
bar_plot = plt.bar(df.index, means, edgecolor='indigo',
                   color=[plt.cm.inferno(i / df.shape[0]) for i in range(df.shape[0])])
plt.xticks(df.index)
ax = plt.gca()
ax.yaxis.set_minor_locator(FixedLocator(means))
ax.yaxis.set_minor_formatter(FormatStrFormatter("%.2f"))
ax.tick_params(axis='y', which='minor', length=40, color='r', labelcolor='r', labelleft=True)
plt.grid(axis='y', which='minor', color='r', ls='--')
plt.tight_layout()
plt.show()

示例图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 TreeExplainer shap 图表的 y 轴标签创建列表? - How to create a list with the y-axis labels of a TreeExplainer shap chart? 如何在时间线图中显示 Matplotlib 中的所有 Y 轴标签? - How to show all Y-Axis Labels in Matplotlib in TimeLine Chart? 如何将第二个 y 轴添加到条形图? 牵牛星 - How to add second y-axis to the bar chart? Altair Matplotlib:一个 X 轴,带有两个不同类型(整数和字符串)的 x 标签,条形图中带有一个 Y 轴 - Matplotlib: One X-Axis with two x labels of different type (int and string) in bar chart with one Y-Axis 堆叠条形图交换 x 轴和 y 轴 - Stacked bar chart swap x-axis and y-axis 如何用pyplot在相同的x轴(日期时间)但不同的y轴上绘制线形图和条形图? - How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot? python - 如何在x轴上的值范围上制作条形图并计算y轴上的范围? - How to make a bar chart on range of values on x-axis and count on the range on y-axis in python? 设置y轴标签,范围,条形图python的限制 - Setting y-axis labels, range, limit on bar graph python Plotly:按值对堆积条形图的 y 轴条进行排序 - Plotly: Sorting the y-axis bars of a stacked bar chart by value Python 条形图 y 轴与平均值 - Python Bar Chart y-axis with value mean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM