0

I´m trying to create some graphs from a dataframe I imported. The problem is I can create an only image with both graphs. I have this output: enter image description here

And I´m looking for this Output: enter image description here

Here is the code:

from pandas_datareader import data
import pandas as pd
import datetime
import matplotlib.pyplot as plt
df = pd.read_csv('csv.csv', index_col = 'Totales', parse_dates=True)
df.head()
df['Subastas'].plot()
plt.title('Subastadas')
plt.xlabel('Fechas')
plt.ylabel('Cant de Subastadas')
plt.subplot()

df['Impresiones_exchange'].plot()
plt.title('Impresiones_exchange')
plt.xlabel('Fechas')
plt.ylabel('Cant de Impresiones_exchange')
plt.subplot()
plt.show()

CSV data:

Totales,Subastas,Impresiones_exchange,Importe_a_pagar_a_medio,Fill_rate,ECPM_medio
Total_07/01/2017,1596260396,30453841,19742.04,3.024863813,0.733696498
Total_07/12/2017,1336604546,57558106,43474.29,9.368463445,0.656716233
Total_07/01/2018,1285872189,33518075,20614.4,4.872889166,0.678244085

Also, I would like to save the output in an xlsx file too!

Martin Bouhier
  • 361
  • 2
  • 6
  • 19

1 Answers1

1

Use plt.subplots() to define two separate Axes objects, then use the ax argument of df.plot() to associate a plot to an axis:

import pandas as pd
import matplotlib.pyplot as plt

f, (ax1, ax2) = plt.subplots(2,1,figsize=(5,10))

df['Impresiones_exchange'].plot(ax=ax2)
ax1.set_title('Impresiones_exchange')
ax1.set_xlabel('Fechas')
ax1.set_ylabel('Cant de Impresiones_exchange')

df['Subastas'].plot(ax=ax1)
ax2.set_title('Subastadas')
ax2.set_xlabel('Fechas')
ax2.set_ylabel('Cant de Subastadas')

enter image description here

andrew_reece
  • 20,390
  • 3
  • 33
  • 58
  • Thanks!!! And how can I change the positions for example one graph next to the other? – Martin Bouhier Jan 11 '18 at 04:54
  • You're welcome. To switch to side-by-side, change the row and column arguments in `plt.subplots()`, e.g. `plt.subplots(1,2)`. Here are [several examples](https://matplotlib.org/examples/pylab_examples/subplots_demo.html) from the pyplot docs. – andrew_reece Jan 11 '18 at 04:56
  • Great! The last question... I would like to have the same values I have in the data frame in the graphs right not 3.0....3.5.... ext – Martin Bouhier Jan 11 '18 at 05:00
  • Do you mean you do not want the `1e9` and `1e7` indicated on the y-axis, but instead you want the full number like `1596260396`? – andrew_reece Jan 11 '18 at 05:01
  • exactly! That is – Martin Bouhier Jan 11 '18 at 05:02
  • You can use `ax1.ticklabel_format(useOffset=False, style='plain')`. This is answered [here](https://stackoverflow.com/a/28373421/2799941). – andrew_reece Jan 11 '18 at 05:05
  • Thanks! I promiss this is the last question... if I would like to add a new graph on the image as ax3, what I need to change of your answer? Because I have an error that shows me "ax3 is not defined" – Martin Bouhier Jan 11 '18 at 05:08
  • You need to add either a row or a column to the `subplots()` argument, like `f, (ax1, ax2, ax3) = plt.subplots(3,1)`. You should really read the examples and documentation, it's pretty useful. There are also a lot of questions on StackOverflow that address these issues. – andrew_reece Jan 11 '18 at 05:09
  • Great! Thanks for all! You was very helpfull!! – Martin Bouhier Jan 11 '18 at 05:18