0

I am trying to plot two histograms with the same date in x-axis. The graph shows date in x-axis right, but failed to show data.

This is the code I used,

import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime

#makes the data

y1 = np.array([123, 456, 789, 10, 5, 123, 456, 789, 10, 5])
y2 = np.array([123, 456, 789, 100, 5, 123, 456, 789, 10, 5])
colors = ['b','g']

#plots the histogram
fig, ax1 = plt.subplots()
ax1.hist([y1,y2],color=colors)

datemin = datetime.strptime('2018-01-01', '%Y-%m-%d')
datemax = datetime.strptime('2018-01-10', '%Y-%m-%d')

ax1.set_xlim(datemin, datemax)
ax1.set_ylabel("Count")

plt.tight_layout()
plt.show()

The figure showed without content.

Jiancong
  • 67
  • 1
  • 7
  • What exactly are you trying to do here? A histogram shows the data on the x axis; your data ranges between 5 and 789, so the histogram should be in that range and not in the date range between the first couple of days of the year 2018. You need to set the limits to `ax1.set_xlim(5, 789)` to see the histogram. – ImportanceOfBeingErnest Mar 24 '18 at 14:53
  • The plot should have datetime range from '2018-01-01' to '2018-01-10' in x-axis, and there are two y-axis values, y1 and y2, which shared the same date time range but in two different values. I want to plot the two y values into the same plot. – Jiancong Mar 25 '18 at 13:47
  • As explained, a histogram has the data on the x axis. I interpreted your question that you want to plot a `bar` plot instead of a `hist`. That is shown in the duplicate. – ImportanceOfBeingErnest Mar 25 '18 at 13:53

0 Answers0