A horizontal stack chart in python giving multiple charts in a Jupyter Notebook

I am trying to create a horizontal bar chart with the specified size, name and title of a legend in Jupyter laptops. When I use other solutions for, I get several graphs printed instead of one. Here's a simplified example:

import pandas as pd
import matplotlib.pyplot as plt
a = [3,5,4,2,1]
b = [3,4,5,2,1]
c = [3,5,4,6,1]
df = pd.DataFrame({'a' : a,'b' : b, 'c' : c})
df.plot.barh(stacked=True);

fig, ax = plt.subplots()
fig.set_size_inches(6,6)

ax.set_title("My ax title")
#plt.title("My plt title") # This seems to be identical to ax.set_title
# Which is prefered?
ax.legend(loc='upper left')

plt.show()

This code gives me the following two graphs. The plot is what I'm looking for, but my size and location of the legend are ignored, and the title was placed on the second graph, which I do not want.

enter image description here enter image description here

Note. I use plot.barh from pandas because I got it to work, but I would be just as happy to do it directly from matplotlib.

+4
source share
2

plot ax. , .

a = [3,5,4,2,1]
b = [3,4,5,2,1]
c = [3,5,4,6,1]
df = pd.DataFrame({'a' : a,'b' : b, 'c' : c})
ax = df.plot.barh(stacked=True);

ax.figure.set_size_inches(6,6)

ax.set_title("My ax title")
ax.legend(loc='upper left')

enter image description here


, , ax plot.

a = [3,5,4,2,1]
b = [3,4,5,2,1]
c = [3,5,4,6,1]
df = pd.DataFrame({'a' : a,'b' : b, 'c' : c})

fig, ax = plt.subplots()
fig.set_size_inches(6,6)

df.plot.barh(stacked=True, ax=ax);

ax.set_title("My ax title")
ax.legend(loc='upper left')

enter image description here

+4

,

df.plot.barh(stacked=True)

,

fig, ax = plt.subplots()

,

,

df.plot.barh(stacked=True,title = "My ax title", figsize = (6,6))
+2

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


All Articles