Skip to content
Advertisement

Matplotlib: I don’t know if I’m plotting in the correct way

When I need to plot 2 series on y axis, I don’t know if this is the best way. It works with lines, but it does not work with line + bars. If I set y2 kind='bar', this serie disappears. indexesM is my dataframe. This is the code:

x = indexesM.index.to_timestamp()
y0 = indexesM['cdi t12m']
y1 = indexesM['ipca t12m']
y2 = indexesM['br real yield t12m']
fig, ax = plt.subplots()
ax.plot(x, y0, label='CDI')
ax.plot(x, y1, label='IPCA')
ax.plot(x, y2, label='Juro real')
plt.show()

Advertisement

Answer

I created a composite graph by customizing a sample from the official reference. It uses ax.bar().

import matplotlib.pyplot as plt

data = {'apple': 10, 'orange': 15, 'lemon': 5, 'lime': 20}
names = list(data.keys())
values = list(data.values())

fig, ax = plt.subplots(figsize=(9, 3))
ax.bar(names, values, color='g')
ax.scatter(names, values, color='r', s=30)
ax.plot(names, values, color='b', lw=2)

ax.set_title('Categorical Plotting')

plt.show()

enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement