Matplotlib - Наложение графиков, но с разным размером окна - PullRequest
1 голос
/ 07 ноября 2019

Я строю графики Revenues и Volume по dates, с Revenues как graph и Volume как bar. Все, что я хочу, это то, что bars должно быть нанесено на нижний 30% графика и не охватывает весь график. Я думаю, это можно сделать с помощью matplotlib.transforms.Bbox, но я не знаю как. Ниже приведен код:

Данные можно найти here.

import matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize']=(20,10) # set the figure size
plt.style.use('fivethirtyeight') # using the fivethirtyeight matplotlib theme

sales = pd.read_csv('sales.csv') # Read the data in
sales.Date = pd.to_datetime(sales.Date) #set the date column to datetime
sales.set_index('Date', inplace=True) #set the index to the date column
print(sales)

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()  # set up the 2nd axis
ax1.plot(sales.Sales_Dollars) #plot the Revenue on axis #1

#ax2.set_position(matplotlib.transforms.Bbox([[0.125,0.1],[0.9,0.32]]))
ax2.bar(sales.index, sales.Quantity,width=20, alpha=0.2, color='orange')
ax2.grid(b=False) # turn off grid #2

ax1.set_title('Monthly Sales Revenue vs Number of Items Sold Per Month')
ax1.set_ylabel('Monthly Sales Revenue')
ax2.set_ylabel('Number of Items Sold')

plt.show()
print('Done!')

Ниже приведен график. Я хочу, чтобы bars отображался только в поле red (внизу 30% высоты), которое я отметил, вместо того, чтобы охватывать всю высоту. Может быть, я должен сделать что-то вроде ax2.set_position(matplotlib.transforms.Bbox([[...],[...]])), но не знаю как !!

plot

1 Ответ

0 голосов
/ 07 ноября 2019
fig, ax1 = plt.subplots()
bb = ax1.get_position()
ax2 = fig.add_axes([bb.x0, bb.y0, bb.width, bb.height*0.3])
ax2.yaxis.tick_right()
ax2.xaxis.set_visible(False)
ax2.spines['top'].set_visible(False)

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...