Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Bug]: Date causing incorrect display for Bar chart #28369

Open
@idflores

Description

@idflores

Bug summary

I have a Pandas Series with monthly dates. The values are real gdp percentages. When trying to plot a bar chart, the chart is completely incorrect only showing small lines in incorrect locations.

Code for reproduction

importmatplotlib.pyplotaspltimportpandasaspdimportnumpyasnpfig,ax=plt.subplots()data=pd.Series([5.2,np.nan,np.nan,6.2,np.nan,np.nan,3.3,np.nan,np.nan,7. ,np.nan,np.nan,-2. ,np.nan,np.nan,-0.6,np.nan,np.nan,2.7,np.nan,np.nan,2.6,np.nan,np.nan,2.2,np.nan,np.nan,2.1,np.nan,np.nan,4.9,np.nan,np.nan,3.4,np.nan,np.nan,1.3,np.nan,np.nan,np.nan,np.nan])dates=pd.date_range(start='2021-01',periods=len(data),freq='M')data.index=pd.to_datetime(dates,format='%Y-%m')data=data.dropna()# removing this line has no effect, the behavior is consistentax.bar(data.index,data.values)# add `width=20` param to get correct outputplt.show()

Actual outcome

image

Expected outcome

image

Additional information

Initially I believed this had to do with the NaN values in the data. (My charting purposes are monthly not quarterly). Even removing NaN values withffill() ordropna() did not produce better results.

Further debugging led me to consider if this was an issue with the dates and how they were formatted. I tried many different ways including manually setting the dates withmatplotlib.ma.date2num thenplot.gca().xaxis.set_major_formatter() but this didn't help either.

Switching to a standardplot() instead ofbar() was even more inconsistent but worked if I set the NaN values to 0.

Eventually, I foundmanually setting thewidth=20 parameter caused the bar chart to behave correctly.

Operating system

Google Colaboratory

Matplotlib Version

3.7.1

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.10.12

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp