2

I have two data frames with i need to plot in one axis. Unfortunately one of displayed data and xticks are shifted to the left.

Test example:

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

df = pd.DataFrame()
df['x'] = np.linspace(20, 30, 10)
df['y'] = np.random.randint(1, 10, 10)

df2 = pd.DataFrame()
df2['x'] = np.linspace(1, 40, 50)
df2['y'] = np.random.randint(1, 10, 50)

fig, ax = plt.subplots()
df.plot(x='x', y='y', kind='bar', ax=ax, width=1., figsize=(3, 2.5), legend=None)
df2.plot(x='x', y='y', kind='line', ax=ax, legend=None, color='red')

plt.show()

Result:

enter image description here

Question: How to proper display this data on one axis?

William Miller
  • 9,839
  • 3
  • 25
  • 46
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38
  • it looks like it's just an issue with the axis formatting, take a look at this question. https://stackoverflow.com/questions/38810009/matplotlib-plot-bar-and-line-charts-together – John T Jan 03 '20 at 20:32

1 Answers1

3

I was only able to solve this by doing the plot in matplotlib directly:

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

df = pd.DataFrame()
df['x'] = np.linspace(20, 30, 10)
df['y'] = np.random.randint(1, 10, 10)

df2 = pd.DataFrame()
df2['x'] = np.linspace(1, 40, 50)
df2['y'] = np.random.randint(1, 10, 50)

fig, ax = plt.subplots()

ax.bar(x=df.x, height=df.y)
ax.plot(df2.set_index('x'), c='red')

enter image description here

ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65