Python Matplotlib:如何减少非数字 tyoe 的 x 刻度线数量 [英] Python Matplotlib: how to reduce number of x tick marks for non-numeric tyoe

查看:65
本文介绍了Python Matplotlib:如何减少非数字 tyoe 的 x 刻度线数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个非常简单的pandas数据框,我正在尝试针对'Adj Close'列绘制 index列(日期,格式为字符串).

 调整关闭日期2010-01-01 1.4389942010-01-04 1.4423982010-01-05 1.4365962010-01-06 1.4404032010-01-07 1.431803...更多行

这是我非常简单的代码:

 将matplotlib.pyplot导入为plt无花果,ax = plt.subplots()ax.plot(df ['Adj Close'],label ='Close Price History')fig.autofmt_xdate()

但是,从重叠x刻度太多的角度来看,该图形令人不快.

我已经使用代码在此处尝试了解决方案

ax.locator_params(axis='x',nbins=10)

我认为这会给我 11 个滴答声,中间有 10 个等距的间隔.但是我遇到了一个错误:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:4: UserWarning: 'set_params()' 没有为 

<块引用>

我不知道错误是什么意思,猜测是因为我的 x 轴是以字符串格式(非数字)?只是强调,我真的在寻找对于解决此问题的简单解决方案,对格式化不感兴趣将专栏作为日期并经历日期中的所有困难操纵.假设我的 x 刻度是字符串,没有办法对它们应用数值计算,是否可以减少显示出x刻度的频率以获得更具视觉吸引力的情节?

解决方案

您可以对刻度进行采样并将列表设置为新刻度.

# 当前 x 个标签current_ticks = ax.get_xticklabels()#每隔一个标签重新采样一次,更改mod(2)以减少采样custom_ticks = [如果current_ticks.index(a)%2为current_ticks中的a.get_text()#设置为新标签ax.set_xticks(custom_ticks)ax.set_xticklabels(custom_ticks)

I have this very simple pandas dataframe here and i'm trying to plot the index column (Date, which is formatted as string) against 'Adj Close' Column.

            Adj Close
      Date                  
2010-01-01  1.438994
2010-01-04  1.442398
2010-01-05  1.436596
2010-01-06  1.440403
2010-01-07  1.431803
... a lot more rows

Here's my very simple code:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(df['Adj Close'], label='Close Price History')
fig.autofmt_xdate()

however the graph is unpleasant in the sense that there are too many overlapping x ticks.

I've tried out the solution here using the code

ax.locator_params(axis='x',nbins=10)

I thought this would give me 11 ticks with 10 equally spaced interval in between. However the I've gotten an error:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:4: UserWarning: 'set_params()' not defined for locator of type <class 'matplotlib.category.StrCategoryLocator'> after removing the cwd from sys.path.

I have no idea what the error means, guess it's because my x-axis is in string format (non-numeric)? Just to emphasis, i'm really seeking for simple solution to solve this problem, not interested to format the column as date and go though all the hardship in date manipulation. Just assuming my x ticks are strings and there's no way to apply numeric calculation on them, is it possible to just reduce the frequency of x ticks shown to get a more visually appealing plot?

解决方案

You can sample your ticks and set the list as your new ticks.

# current x labels
current_ticks = ax.get_xticklabels()
# resampling every other label, change mod(2) to sample less
custom_ticks = [a.get_text() for a in current_ticks if current_ticks.index(a)%2] 
# set as new labels
ax.set_xticks(custom_ticks)
ax.set_xticklabels(custom_ticks)

这篇关于Python Matplotlib:如何减少非数字 tyoe 的 x 刻度线数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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