[英]Bigger x-axis range in seaborn plot
I'm plotting a bar graph with matplotlib/seaborn and the range is the min/max values, plus it skips values that have 0 counts. 我正在用matplotlib / seaborn绘制条形图,范围是最小/最大值,加上它会跳过具有0个计数的值。 How can I both pad the range and not skip 0 values?
如何既填充范围又不跳过0值?
# bar
ax2 = figDayMonth.add_subplot(2,1,2)
ax2 = sns.countplot(x=np.asarray(dayMonth), palette="pastel")
ax2.set_title('Days of Month Counts', FontSize=20)
ax2.tick_params(labelsize=15)
ax2.set_ylabel("Count", FontSize=16)
ax2.set_xlabel("Day of Month", FontSize=16)
sns.despine(left=True, bottom=True, top=True, right=True)
plt.show()
print(2*'\n')
dayMonth
is a list of integers with the above counts. dayMonth
是具有上述计数的整数列表。 Although for example there are no 2, 29, or 30 values, I'd still like the graph to reserve a place for those values. 尽管例如没有2、29或30个值,但我仍希望该图为这些值保留一个位置。
I tried ax2.set_xticks(np.arange(32))
but that just seems to squeeze my graph to the left without changing the x-axis values. 我尝试了
ax2.set_xticks(np.arange(32))
但这似乎只是在不更改x轴值的情况下将我的图向左挤压。
Essentially you want to plot a histogram of the input list. 本质上,您想绘制输入列表的直方图。 That histogram can be created using
该直方图可以使用
x = np.arange(1,33)
plt.hist(dayMonth, bins=x)
A full example would be 一个完整的例子是
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
dayMonth = [1,3,12,30,2,3,12,16,18,31,3,13,16,18,30,1,3,12,16,18,30]
x = np.arange(1,33)
_,_,bars = plt.hist(dayMonth, bins=x, align="left")
colors=sns.color_palette(palette="pastel", n_colors=len(x))
for color, bar in zip(colors, bars):
bar.set_color(color)
plt.gca().set_xlim(-1,32)
plt.xticks(x[:-1])
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.