python - 向多水平条形图添加垂直线
问题描述
我正在生成一个多条形图作为水平条形图,我现在需要做的是在每个 y 的特定位置(基于 x 轴值)的每个水平条中注释(基本上创建一条垂直线) -axis,其中 y 轴是分类(名称),x 轴是数字(整数)s。
我看过axis.vlines
但无法正常工作。
import seaborn as sns
import matplotlib.pyplot as plt
crashes = sns.load_dataset("car_crashes").sort_values("total", ascending=False)
crashes['max_range'] = crashes['total'] * 0.85
sns.set_color_codes("muted")
sns.set(style="whitegrid")
sns.barplot(x="total", y="abbrev", data=crashes, label="", color="r")
sns.barplot(x="max_range", y="abbrev", data=crashes, label="", color="y")
sns.barplot(x="alcohol", y="abbrev", data=crashes,label="normal range", color="g")
#dummy data for the "vertical lines" i want to plot
crashes['actual'] = crashes['alcohol'] * 1.85
上面的代码创建了一个像这样的图:
https://seaborn.pydata.org/examples/horizontal_barplot.html
现在我基本上想从底层数据框的另一列中的每一行添加一条垂直线(因此对于图中的每个条)。
解决方案
axis.vlines
足以胜任这项工作。为此,我首先提取条形图标签的 y 点。比我为这些点制作一个 x 值的字典。比我用来axis.vlines
在条上画一条红线。
import seaborn as sns
import matplotlib.pyplot as plt
crashes = sns.load_dataset("car_crashes").sort_values("total", ascending=False)
crashes['max_range'] = crashes['total'] * 0.85
sns.set_color_codes("muted")
sns.set(style="whitegrid")
# Store the returned axes in a variable
ax = sns.barplot(x="total", y="abbrev", data=crashes, label="", color="r")
ax = sns.barplot(x="max_range", y="abbrev", data=crashes, label="", color="y")
ax = sns.barplot(x="alcohol", y="abbrev", data=crashes,label="normal range", color="g")
#dummy data for the "vertical lines" i want to plot
crashes['actual'] = crashes['alcohol'] * 1.85
#### MY ADDITIONS ####
# Form dictionary of bar chart keys (i.e. Y axis data, here it is "abbrev") to
# corresponding y and x points
y_labs = list(ax.get_yticklabels())
y_tic_pos = list(ax.get_yticks())
y_tick_vals = {}
for i in range(len(y_tic_pos)):
y_tick_vals[y_labs[i].get_text()] = y_tic_pos[i]
x_points = {lab:crashes[crashes["abbrev"] == lab]["actual"].values[0] for lab in y_tick_vals}
# for each of the relevant y axis, draw a vertical line
for key in y_tick_vals:
c_y = y_tick_vals[key]
c_x = x_points[key]
# I just did some trial and error to find out that each bar is 0.5 wide;
# this may not be the case for other plots.
c_ymin = c_y - 0.25
c_ymax = c_y + 0.25
ax.vlines(c_x, c_ymin, c_ymax, colors="r")
plt.show()
推荐阅读
- c# - 获取 Json 数组大小
- python - 查找具有可自定义数字之间距离的数字运行
- php - PHP如何创建一个具有1个或多个键相同的新多维数组?
- android - Android Studio Editor foreground color
- c++ - On OpenGL is it possible to have a small viewport map to the entire window?
- debugging - React Native Debugger Won't Turn Off
- php - 如何使用数据库数据填充 html 上的选择下拉列表
- r - R raster plot even legend
- java - JAVA中的JPanel里面的JPanel
- powershell - 比较字符串与数组