Mismatch when adjusting shape size using pandas construction method

I am trying to use the convenience of the pandas frame construction method when adjusting the size of the created shape. (I save the data in a file, as well as output them to a line in a Jupyter laptop). I found a method that was successful in most cases, except when I draw two lines on the same chart - then the number returns to the default size.

I suspect that this may be due to differences between the schedule of the series and the graphics on the data frame.

Example installer code:

data = {
    'A': 90 + np.random.randn(366),
    'B': 85 + np.random.randn(366)
}

date_range = pd.date_range('2016-01-01', '2016-12-31')

index = pd.Index(date_range, name='Date')

df = pd.DataFrame(data=data, index=index)

Control - this code generates the expected result (wide graph):

fig = plt.figure(figsize=(10,4))

df['A'].plot()
plt.savefig("plot1.png")
plt.show()

Result:

plot1.png

Building two lines - the size of the figure is not equal (10.4)

fig = plt.figure(figsize=(10,4))

df[['A', 'B']].plot()
plt.savefig("plot2.png")
plt.show()

Result:

plot2.png

, , ?

+4
2

pandas.DataFrame.plot(). , .

matplotlib fig = plt.figure(figsize=(10,4)), DataFrame. pandas plot , , matplotlib, , . .

. , .. pandas , . , , figsize.

, df[['A', 'B']].plot(figsize=(10,4)). , . 2 , , , . , python script plt.show() , .

, , pandas ,

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"A":[2,3,1], "B":[1,2,2]})
df[['A', 'B']].plot(figsize=(10,4))

plt.show()

, ax pandas.DataFrame.plot(ax=ax), ax - . , plt.gca().

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"A":[2,3,1], "B":[1,2,2]})
plt.figure(figsize=(10,4))
df[['A', 'B']].plot(ax = plt.gca())

plt.show()

- PaulH.

+5

Figure Axes. pyplot. :

fig1, ax1 = plt.subplots(figsize=(10,4))
df['A'].plot(ax=ax1)
fig1.savefig("plot1.png")


fig2, ax2 = plt.figure(figsize=(10,4)) 
df[['A', 'B']].plot(ax=ax2)
fig2.savefig("plot2.png")

plt.show()
+3

Source: https://habr.com/ru/post/1669639/


All Articles