控制 pyplot 中 x 滴答的数量 [英] controlling the number of x ticks in pyplot

查看:33
本文介绍了控制 pyplot 中 x 滴答的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示所有13个x滴答声,但该图仅显示其中7个具有两个间隔.

<块引用>

  plt.locator_params(axis ='x',nbins = 13)

为什么上面的代码不起作用??

将 numpy 导入为 np导入matplotlib.pyplot作为plt将熊猫作为pd导入导入 matplotlib.dates 作为日期y = [0、0.86、0.826、0.816、0.807、0.803、0.804、0.803、0.802、0.81、0.813、0.813、0.813]times = pd.date_range('2015-02-25', period=13)图, ax = plt.subplots(1)fig.autofmt_xdate()xfmt = dates.DateFormatter('%d-%m-%y')ax.xaxis.set_major_formatter(xfmt)plt.locator_params(axis='x',nbins=13)ax.plot_date(times.to_pydatetime(),y,'v-')ax.xaxis.set_minor_locator(dates.WeekdayLocator(byweekday=(1),间隔= 1))ax.xaxis.set_minor_formatter(dates.DateFormatter('%d \ n%a'))ax.xaxis.grid(True, which="minor")ax.yaxis.grid()plt.tight_layout()plt.show()

解决方案

警告应该给你一些线索,为什么会发生这种情况:

UserWarning: 'set_params()' 没有为 

使用 plt.xticks(times.to_pydatetime())代替:

将 numpy 导入为 np导入matplotlib.pyplot作为plt将熊猫作为pd导入导入 matplotlib.dates 作为日期y = [0、0.86、0.826、0.816、0.807、0.803、0.804、0.803、0.802、0.81、0.813、0.813、0.813]times = pd.date_range('2015-02-25',period = 13)无花果,ax = plt.subplots(1)fig.autofmt_xdate()xfmt = dates.DateFormatter('%d-%m-%y')ax.xaxis.set_major_formatter(xfmt)ax.plot_date(times.to_pydatetime(),y,'v-')ax.xaxis.set_minor_locator(dates.WeekdayLocator(byweekday=(1),间隔= 1))plt.xticks(times.to_pydatetime())ax.xaxis.set_minor_formatter(dates.DateFormatter('%d\n%a'))ax.xaxis.grid(True,which ="minor")ax.yaxis.grid()plt.tight_layout()plt.show()

I want to display all 13 x ticks, but the graph only shows 7 of them having two intervals.

plt.locator_params(axis='x',nbins=13)

Why doesn't above code work??

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

y = [0, 0.86, 0.826, 0.816, 0.807, 0.803, 0.804, 0.803, 0.802,0.81, 0.813, 0.813, 0.813]

times = pd.date_range('2015-02-25', periods=13)
fig, ax = plt.subplots(1)
fig.autofmt_xdate()

xfmt = dates.DateFormatter('%d-%m-%y')
ax.xaxis.set_major_formatter(xfmt)

plt.locator_params(axis='x',nbins=13)

ax.plot_date(times.to_pydatetime(), y, 'v-')
ax.xaxis.set_minor_locator(dates.WeekdayLocator(byweekday=(1),
                                            interval=1))
ax.xaxis.set_minor_formatter(dates.DateFormatter('%d\n%a'))
ax.xaxis.grid(True, which="minor")
ax.yaxis.grid()
plt.tight_layout()
plt.show()

解决方案

The warning should give you some clue why this is happening:

UserWarning: 'set_params()' not defined for locator of type <class 'pandas.tseries.converter.PandasAutoDateLocator'>
  str(type(self)))

Use plt.xticks(times.to_pydatetime()) instead:

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

y = [0, 0.86, 0.826, 0.816, 0.807, 0.803, 0.804, 0.803, 0.802,0.81, 0.813, 0.813, 0.813]

times = pd.date_range('2015-02-25', periods=13)
fig, ax = plt.subplots(1)
fig.autofmt_xdate()

xfmt = dates.DateFormatter('%d-%m-%y')
ax.xaxis.set_major_formatter(xfmt)


ax.plot_date(times.to_pydatetime(), y, 'v-')
ax.xaxis.set_minor_locator(dates.WeekdayLocator(byweekday=(1),
                                            interval=1))
plt.xticks(times.to_pydatetime())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%d\n%a'))
ax.xaxis.grid(True, which="minor")
ax.yaxis.grid()
plt.tight_layout()
plt.show()

这篇关于控制 pyplot 中 x 滴答的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆