matplotlib散点图-x轴上仅显示一些标签

时间:2019-07-19 07:05:11

标签: matplotlib scatter-plot

在matplotlib中使用散点图进行绘制时,我发现标签中缺少一些x轴值。我希望所有的x轴图例都显示在图中。

这可能与刻度间距有关,但是我不确定如何设置它以显示所有x轴值。

在示例代码中,我希望所有日期都显示在x轴上

x = [datetime.date(2019, 6, 16), datetime.date(2019, 6, 17), datetime.date(2019, 6, 18), datetime.date(2019, 6, 19),
     datetime.date(2019, 6, 20), datetime.date(2019, 6, 21), datetime.date(2019, 6, 22), datetime.date(2019, 6, 23),
     datetime.date(2019, 6, 24), datetime.date(2019, 6, 25), datetime.date(2019, 6, 26), datetime.date(2019, 6, 27),
     datetime.date(2019, 6, 28), datetime.date(2019, 6, 29), datetime.date(2019, 6, 30), datetime.date(2019, 7, 1),
     datetime.date(2019, 7, 2), datetime.date(2019, 7, 3), datetime.date(2019, 7, 4), datetime.date(2019, 7, 5),
     datetime.date(2019, 7, 6), datetime.date(2019, 7, 7), datetime.date(2019, 7, 8), datetime.date(2019, 7, 9),
     datetime.date(2019, 7, 10), datetime.date(2019, 7, 11), datetime.date(2019, 7, 12), datetime.date(2019, 7, 13),
     datetime.date(2019, 7, 15)]
y = [0.15338331291011087, 0.15340904024033467, 0.1534195786228156, 0.15343290378685995, 0.15331644003478487,
     0.1533570064827251, 0.1531156771286262, 0.15307150988142237, 0.15306137109205153, 0.15302301551230038,
     0.15295889536607005, 0.15298157619113423, 0.15286883583977182, 0.15283539558962958, 0.15284508041253356,
     0.15281542656182034, 0.1527844647725921, 0.15277054534676898, 0.1527339281127108, 0.15270419704783855,
     0.15261812595095475, 0.15255120245035042, 0.15251650362641, 0.15257536163149088, 0.15253967278547242,
     0.15249871561808356, 0.15248591103997422, 0.15242121840852002, 0.15248773465596907]



fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(x, y, s=10, c='b', marker="s", label='y')
plt.legend(loc='upper left')
plt.xticks(rotation=90)
plt.show()

Plot that I get with the sample code

1 个答案:

答案 0 :(得分:0)

只需在plt.xticks()中传递 x的值,然后使用'plt.gcf'设置x轴即可。
我为x创建了一个随机列表,并绘制了图表进行检查。

from matplotlib import pyplot as plt
from datetime import datetime
def std(a):
    return datetime.strptime(a, '%Y, %m, %d').date()

x = [std('2019, 6, 16'), std('2019, 6, 17'), std('2019, 6, 18'), std('2019, 6, 19'),
     std('2019, 6, 20'), std('2019, 6, 21'), std('2019, 6, 22'), std('2019, 6, 23'),
     std('2019, 6, 24'), std('2019, 6, 25'), std('2019, 6, 26'), std('2019, 6, 27'),
     std('2019, 6, 28'), std('2019, 6, 29'), std('2019, 6, 30'), std('2019, 7, 1'),
     std('2019, 7, 2'), std('2019, 7, 3'), std('2019, 7, 4'), std('2019, 7, 5'),
     std('2019, 7, 6'), std('2019, 7, 7'), std('2019, 7, 8'), std('2019, 7, 9'),
     std('2019, 7, 10'), std('2019, 7, 11'), std('2019, 7, 12'), std('2019, 7, 13'),
     std('2019, 7, 15')]
y = [0.15338331291011087, 0.15340904024033467, 0.1534195786228156, 0.15343290378685995, 0.15331644003478487,
     0.1533570064827251, 0.1531156771286262, 0.15307150988142237, 0.15306137109205153, 0.15302301551230038,
     0.15295889536607005, 0.15298157619113423, 0.15286883583977182, 0.15283539558962958, 0.15284508041253356,
     0.15281542656182034, 0.1527844647725921, 0.15277054534676898, 0.1527339281127108, 0.15270419704783855,
     0.15261812595095475, 0.15255120245035042, 0.15251650362641, 0.15257536163149088, 0.15253967278547242,
     0.15249871561808356, 0.15248591103997422, 0.15242121840852002, 0.15248773465596907]



fig = plt.figure(figsize=(8,5))
ax1 = fig.add_subplot(111)
ax1.scatter(x, y, s=10, c='b', marker="s", label='y')
plt.legend(loc='upper left')
#plt.xticks(x,rotation=90)
#plt.xticks(range(len(x)))

plt.gca().margins(x=0)
plt.gcf().canvas.draw()
t_l = plt.gca().get_xticklabels()
maxsize = max([t.get_window_extent().width for t in t_l])
m = .2 # inch margin
s = maxsize/plt.gcf().dpi*len(x)+3*m
margin = m/plt.gcf().get_size_inches()[1]

plt.gcf().subplots_adjust(left=margin, right=0.8-margin)
plt.gcf().set_size_inches(s, plt.gcf().get_size_inches()[1])
plt.xticks(x,rotation=90)
plt.show()

output