this is my first question on the forum.
I am using Pandas to read a.dta database. In this database, I have the answers to a survey carried out in different years. In this case 2011-2012-2013-2014.
What I want to do is a horizontal bar chart with the crossing of two variables, but that the result for each year is seen in the same chart, instead of making a chart for each of the years that the survey was conducted.
Is this possible to do with Matplotlib?
Thank you all very much in advance.
you can plot your graph horizontally by using this code
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
plt.rcdefaults()
fig, ax = plt.subplots()
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
ax.barh(y_pos, performance, xerr=error, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')
plt.show()
Copy to clipboard
Welcome to stackoverflow my friend. As an advice, it would be better you show a piece of your data frame. I created a dummy dataframe like this;
First, you need to turn your year variable to categorical(just an advice)
df["Year"] = df["Year"].astype('category')
Then I used this piece of code
ax = df.plot(x="Year", y="Var1", kind="bar")
df.plot(x="Year", y="Var2", kind="bar", ax=ax, color="C2")
the output is like this
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.