Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

python - How to get two columns on secondary axis using matplotlib

I am somewhat new with python plots using matplotlib, so please excuse me. I want to plot a group bar plot, and I want to have two of those bars on secondary axis since those two have values at much lower range compared to other bars in the group. Is it possible? I could not find any solution online or in matplotlib documentation. I do know that we can do secondary axis plot e.g.

df.plot(kind="bar",figsize=(30, 10),secondary_y= 'columnWithLowValueRange', rot = 0)

However, even for one column this option somehow messes up my formatting. e.g the legend box is overlapped with secondary axis legend name and the ylabel for primary axis goes missing etc.

This is what my current plot looks like.enter image description here

As you can see some of the vals are barely visible. So, I would like to plot val6 and val7 on secondary plot.

My current code is like this:

df = df1.loc[tag_df['names'] == max_val]
del df['names']
df.set_index('names', inplace=True)
df.plot(kind="bar",figsize=(30, 10))
plt.title("myPlot", fontsize = 19)
xlabel= plt.xlabel("my x axis", fontsize = 17, color = "dimgray")
plt.ylabel("my y axis", fontsize = 16.5, color = "dimgray")
plt.legend(loc = 'upper right', fontsize = 12)
plt.xticks(fontsize=14, rotation=360)
plt.yticks(fontsize=14)
plt.grid(linestyle ='-', axis = 'y')
plt.savefig("myfig.png")
plt.clf()

df has those val1-val6 columns. The reference plot is excel plot but, my python plot is also very similar.

Any help here is highly appreciated. I have already searched stackoverflow for this, so hopefully it will not be marked as duplicate.

Thanks

question from:https://stackoverflow.com/questions/66057707/how-to-get-two-columns-on-secondary-axis-using-matplotlib

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is how you do it. Since I didn't have your data, I used a different data set. The blue and yellow bars represent the orders and sales and correspond to the left y-axis. The red and green bars represents the year-over-year changes in orders and sales and correspond to the right y-axis. Notice how the legends are correctly placed next to the corresponding axis.

fig = plt.figure(figsize=(12,5))
fig.suptitle("title")
ax1 = fig.add_subplot()
ax2 = ax1.twinx()
ax1.set_ylabel("Ordered, Sold")
ax2.set_ylabel("Year Over Year")

year_indexes = np.arange(len(years))
WIDTH = 0.1
ax1.bar(year_indexes - 2*WIDTH, ordered, width=WIDTH, color="orange", label="Ordered")
ax1.bar(year_indexes - WIDTH, sold, width=WIDTH, color="blue", label="Sold")
ax2.bar(year_indexes + WIDTH, ordered_yoy, width=WIDTH, color ="red", label ="ordered year-over-year")
ax2.bar(year_indexes + 2*WIDTH, sold_yoy, width=WIDTH, color ="green", label ="sold year-over-year")

plt.xticks(year_indexes,years)
ax1.legend(loc = "upper left")
ax2.legend()
plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...