Skip to content

df.plot with two plots in the same axes and 2nd one is line plot makes different chart between 0.23.4 and 0.25.0 #27686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wh243s opened this issue Aug 1, 2019 · 10 comments · Fixed by #27993
Labels

Comments

@wh243s
Copy link

wh243s commented Aug 1, 2019

Code Sample, a copy-pastable example if possible

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('iris.csv')
dfg = df.groupby('species').mean()
dfg['swratio'] = dfg.sepal_width / dfg.sepal_length

fig, ax = plt.subplots(figsize=(10, 6))
dfg[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']].plot.bar(ax=ax, rot=0, stacked=False)
dfg[['swratio']].plot.line(label='Sepal Width Ratio', 
                 ax=ax, secondary_y=True, mark_right=False, 
                 rot=0, marker='o', markersize=3, color='c')
ax.grid(True)
pngfile = 'Output.png'
fig = plt.gcf()
fig.savefig(pngfile, bbox_inches = 'tight', dpi=100)

Problem description

dataframe.plot with secondary_y=True makes different chart between 0.23.4 and 0.25.0 as attached.
GeneratedFromPahdas0 23 4
GeneratedFromPahdas0 25 0

Note: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!

Note: Many problems can be resolved by simply upgrading pandas to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if master addresses this issue, but that is not necessary.

For documentation-related issues, you can check the latest versions of the docs on master here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

@jbrockmendel jbrockmendel added the Visualization plotting label Aug 3, 2019
@TomAugspurger
Copy link
Contributor

@wh243s can you debug to figure out what is going on?

@wh243s
Copy link
Author

wh243s commented Aug 6, 2019

I've tried three different parameters on 0.25.0.

  1. dfg[['swratio']].plot.line(ax=ax, secondary_y=True)
  2. dfg[['swratio']].plot.line(ax=ax)
  3. dfg[['swratio']].plot.line()

The attached are the results and csv being used.
1  GeneratedFromPahdas0 25 0_ax=ax, secondary_y=True
2  GeneratedFromPahdas0 25 0_plot line(ax=ax)
3  GeneratedFromPahdas0 25 0_plot line()

iris _renamed_from_csv.txt

@wh243s
Copy link
Author

wh243s commented Aug 6, 2019

  1. dfg[['swratio']].plot.line() has two chart in png and only the last one shows. The attached is the first one.
    GeneratedFromPahdas0 25 0_plot line()_2

@wh243s
Copy link
Author

wh243s commented Aug 6, 2019

When line and bar charts are switched, it looks fine.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('iris.csv')
dfg = df.groupby('species').mean()
dfg['swratio'] = dfg.sepal_width / dfg.sepal_length

fig, ax = plt.subplots(figsize=(10, 6))
dfg[['swratio']].plot.line(ax=ax, color='c', rot=0)
dfg[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']].plot.bar(ax=ax, secondary_y=True, rot=0)
ax.grid(True)
plt.title('plot.bar(ax=ax, secondary_y=True, rot=0)')
pngfile = 'GeneratedFromPahdas0.25.0_switched_line_and_bar.png'
fig = plt.gcf()
fig.savefig(pngfile, bbox_inches = 'tight', dpi=100)
GeneratedFromPahdas0 25 0_switched_line_and_bar

@wh243s
Copy link
Author

wh243s commented Aug 6, 2019

It happens when the second chart is the line chart in the same axes.

@wh243s wh243s changed the title dataframe.plot with secondary_y=True makes different chart between 0.23.4 and 0.25.0 df.plot with two plots in the same axes and 2nd one is line plot makes different chart between 0.23.4 and 0.25.0 Aug 6, 2019
@arthurire
Copy link

arthurire commented Aug 8, 2019

@TomAugspurger https://github.com/pandas-dev/pandas/issues/27796. Same issue. This also happens when I use seaborn to plot a heatmap.

@TomAugspurger
Copy link
Contributor

You're saying #27796 is a duplicate of this?

@arthurire
Copy link

arthurire commented Aug 8, 2019

You're saying #27796 is a duplicate of this?

I guess so. I mean the results(plots) we get are the same: line charts would re-define the range of x-axis, which makes the line chart starts from the origin. But there should be a certain distance between the line chart and the origin.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Aug 8, 2019 via email

@ryanrolds
Copy link

ryanrolds commented Nov 8, 2019

The fix to this causes broader limits that were set prior to calls to pandas.DataFrame.plot.(line|area) to be lost. The fix is to manually set the desired axis limit after the calls to DataFrame.plot, but it was still a bit of a surprise to have it narrow the x-axis after the was specifically set larger so that projection lines (pyplot.axvline) would be visible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants