简体   繁体   English

如何在 matplotlib 中绘制 200 行(时间序列)并使其在 x 轴上可读?

[英]How can plot 200 rows (time series) in matplotlib and make it readable in the x-axis?

Hey guys this is my code.嘿伙计们,这是我的代码。

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

banco=pd.read_csv('banco central3.csv',index_col='Fecha',parse_dates=['Fecha'], usecols=. 
['RRII','Fecha'])
print(banco.head())
fig, ax = plt.subplots(figsize=(10, 10))

ax.bar(banco.index.values,
    banco['RRII'],
    color='purple')


ax.xaxis.set_major_locator(mdates.WeekdayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%Y'))


ax.set(xlabel="DATE",
   ylabel="RRII",
   title="\nReservas Internacionales")
plt.tight_layout()
plt.show()

The output, as a plot, is the following.作为绘图的输出如下。 The data (x-axis) is unreadable.数据(x 轴)不可读。

The datetime('Fecha') is about 200 rows. datetime('Fecha') 大约有 200 行。 I want to clarity the X axis to become it readable.我想澄清 X 轴以使其可读。

and also i dont know why the barplot looks in that way (so separated among them).而且我也不知道为什么条形图看起来那样(在它们之间如此分离)。 I want a classic barplot.我想要一个经典的条形图。

You see that the datatime is unreadable你看到数据时间不可读

the format of the data is this:数据的格式是这样的:

            RRII
Fecha             
2019-11-27  43.765
2019-11-28  43.736
2019-11-29  43.772
2019-02-12  43.731
2019-03-12  43.750

But i have 200 rows.但我有 200 行。

There is any way of making it?有什么制作方法吗? Thanks you very much非常感谢你

EDIT编辑

I change the mdates.Daylocator to the m.dates.Monthlocator.我将 mdates.Daylocator 更改为 m.dates.Monthlocator。 and i added a Plt.setp function.我添加了一个 Plt.setp 函数。 Now my plot is not overlap but the line plot has no sense.现在我的情节不重叠但线图没有意义。

ax.xaxis.set_major_locator(mdates.MonthLocator())

plt.setp(ax.get_xticklabels(), rotation=30, 
horizontalalignment='right')

IMAGE HERE.图像在这里。 I want the plot follow the RRII by datetime我希望情节按照日期时间遵循 RRII

To make the x axis readable, change the line要使 x 轴可读,请更改行

ax.xaxis.set_major_locator(mdates.WeekdayLocator())

to

ax.xaxis.set_major_locator(mdates.MonthLocator())

However, the usage of barplot is not advisable with 200 rows;但是,对于 200 行,不建议使用 barplot; use a normal plot:使用普通图:

ax.plot(banco['RRII'], '.', color='purple')

[edit: changed marker from line point] [编辑:从线点更改标记]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何绘制时间序列,其中x轴是matplotlib中的datetime.time对象? - how to plot time series where x-axis is datetime.time object in matplotlib? 在Matplotlib图中,x轴上有时间,如何使长轴和短轴标签不重叠? - In a Matplotlib plot with time on the x-axis, how to make the major and minor axis labels not overlap? 如何使plot的x轴原点和y轴原点在matplotlib中重叠? - How can I make the origin of the x-axis and the origin of the y-axis of a plot overlap in matplotlib? 如何在 Python 中将分钟时间设置为 Matplotlib plot 的 x 轴? - How to set minutes time as x-axis of a Matplotlib plot in Python? 如何通过在 matplotlib 中的 x 轴上重复字符串来制作 plot? - how to make a plot by repeating strings on x-axis in matplotlib? 如何在matplotlib中缩小X轴上的图? - How to shrink plot on x-axis in matplotlib? 垂直对齐时间序列(绘图和条形图)在 matplotlib 中共享相同的 x 轴 - Vertically align time series (plot and barplot) sharing same x-axis in matplotlib 如何使时间列成为绘图的 x 轴? - How to make time column to be x-axis of a plot? Matplotlib的X轴图 - X-axis Plot with Matplotlib 使用 Pandas 自相关图 - 如何限制 x 轴以使其更具可读性? - Using Pandas Autocorrelation Plot - how to limit x-axis to make it more readable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM