Nicola Ferrari
Nicola Ferrari

Reputation: 21

Python matplotlib - Two charts instead of one

Why are two graphs created? I would like that instead of being created two separate graphs, only one was created.

import matplotlib.pyplot as pl

x=1,2,3,4
y=3,6,1,9
pl.plot(x, y)

pl.figure(figsize=(7.5, 5), dpi=80)
pl.axis([0,29,0,21])
pl.xticks([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]) 
pl.yticks([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
pl.xlabel("Giornate", fontweight="bold")
pl.ylabel("Ore", fontweight="bold")
pl.title("Febbraio 2019", fontsize=20, fontweight="bold")
pl.grid(b=True, color="gray")
pl.rcParams['axes.facecolor'] = "tan"

pl.show()

Upvotes: 2

Views: 248

Answers (3)

hemanta
hemanta

Reputation: 1510

You just need to change the position; where you call pl.plot as following:

import matplotlib.pyplot as pl
x=1,2,3, 4
y=3,6,1,9
pl.figure(figsize=(7.5, 5), dpi=80)
pl.axis([0,29,0,21])
pl.xticks([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]) 
pl.yticks([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
pl.plot(x, y)
pl.xlabel("Giornate", fontweight="bold")
pl.ylabel("Ore", fontweight="bold")
pl.title("Febbraio 2019", fontsize=20, fontweight="bold")
pl.grid(b=True, color="gray")
pl.rcParams['axes.facecolor'] = "tan" 
pl.show()

Upvotes: 1

TheCoolDrop
TheCoolDrop

Reputation: 1066

First figure gets created when you call pl.plot and the second one gets created when you call pl.figure function.

You need to throw one of them out.

Upvotes: 1

Antoine Soulier
Antoine Soulier

Reputation: 33

If you use plt.figure after calling plt.plot a second figure is created

import matplotlib.pyplot as pl

x=1,2,3,4
y=3,6,1,9


pl.figure(figsize=(7.5, 5), dpi=80)

pl.plot(x, y)

pl.axis([0,29,0,21])
pl.xticks([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]) 
pl.yticks([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
pl.xlabel("Giornate", fontweight="bold")
pl.ylabel("Ore", fontweight="bold")
pl.title("Febbraio 2019", fontsize=20, fontweight="bold")
pl.grid(b=True, color="gray")
pl.rcParams['axes.facecolor'] = "tan"

pl.show()

Upvotes: 0

Related Questions