完整代码如下,怎么将绘制的图形的x轴改为时间呀?现在是将x轴的设置注释掉了如图,一加上,图形就不对了如图
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates
stations_up = ['北京' ,'北京' ,'天津','天津', '石家庄', '石家庄', '邯郸','邯郸']
stations_down=[ '邯郸','邯郸', '石家庄', '石家庄','天津','天津','北京' ,'北京']
trains_up = [
{'schedule': [(0, '08:00:00'), (1, '08:00:30'), (2, '08:01:10'),(3, '08:01:50'),(4, '08:02:50'),(5, '08:03:10'),(6, '08:04:50'),(7, '08:05:50')]},
{'schedule': [(0, '08:01:00'), (1, '08:01:30'), (2, '08:02:10'),(3, '08:02:50'),(4, '08:03:50'),(5, '08:04:10'),(6, '08:05:50'),(7, '08:06:50')]},
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'),(3, '08:03:50'),(4, '08:04:50'),(5, '08:05:10'),(6, '08:06:50'),(7, '08:07:50')]}
]
trains_down=[
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'),(3, '08:03:50'),(4, '08:04:50'),(5, '08:05:10'),(6, '08:06:50'),(7, '08:07:50')]}
]
#对于列车上行
for t in trains_up:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
t['schedule'][i] = (t['schedule'][i][0], dt.hour + dt.minute/60.0+dt.second/3600.0)
for t in trains_up:
times = [x[1] for x in t['schedule']]
plt.plot(times,stations_up,linewidth=1)
#对于下行
for t in trains_down:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
t['schedule'][i] = (t['schedule'][i][0], dt.hour + dt.minute/60.0+dt.second/3600.0)
for t in trains_down:
times = [x[1] for x in t['schedule']]
plt.plot(times,stations_down,linewidth=1)
#横纵轴的基本设置
# times = [ '8:00', '08:10', '08:20', '08:30', '08:40', '08:50', '09:00', '09:10', '09:20', '09:30', '09:40']
# plt.xticks(range(len(times)), times)
plt.rcParams['font.sans-serif'] = ['SimSun'] #设置字体,
plt.rcParams['axes.unicode_minus'] = False #plt.rcParams[‘axes.unicode_minus’]是一个配置参数,它决定了坐标轴上负号的显示方式
plt.xlabel('时间')
plt.ylabel('站点')
plt.title('列车运行图')
plt.legend()
plt.show()
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates
stations_up = ['北京' ,'北京' ,'天津','天津', '石家庄', '石家庄', '邯郸','邯郸']
stations_down=[ '邯郸','邯郸', '石家庄', '石家庄','天津','天津','北京' ,'北京']
trains_up = [
{'schedule': [(0, '08:00:00'), (1, '08:00:30'), (2, '08:01:10'),(3, '08:01:50'),(4, '08:02:50'),(5, '08:03:10'),(6, '08:04:50'),(7, '08:05:50')]},
{'schedule': [(0, '08:01:00'), (1, '08:01:30'), (2, '08:02:10'),(3, '08:02:50'),(4, '08:03:50'),(5, '08:04:10'),(6, '08:05:50'),(7, '08:06:50')]},
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'),(3, '08:03:50'),(4, '08:04:50'),(5, '08:05:10'),(6, '08:06:50'),(7, '08:07:50')]}
]
trains_down=[
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'),(3, '08:03:50'),(4, '08:04:50'),(5, '08:05:10'),(6, '08:06:50'),(7, '08:07:50')]}
]
#对于列车上行
for t in trains_up:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
t['schedule'][i] = (t['schedule'][i][0], dt) # 注意这里修改了时间格式
for t in trains_up:
times = [mdates.date2num(x[1]) for x in t['schedule']] # 使用mdates.date2num转换日期时间
plt.plot(times, stations_up, linewidth=1) # 使用plt.plot来绘制时间数据
#对于下行
for t in trains_down:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
t['schedule'][i] = (t['schedule'][i][0], dt) # 注意这里修改了时间格式
for t in trains_down:
times = [mdates.date2num(x[1]) for x in t['schedule']] # 使用mdates.date2num转换日期时间
plt.plot(times, stations_down, linewidth=1) # 使用plt.plot来绘制时间数据
#横纵轴的基本设置
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) # 使用mdates.DateFormatter设置时间格式
plt.gcf().autofmt_xdate() # 自动旋转x轴的日期标签以避免重叠
plt.rcParams['font.sans-serif'] = ['SimSun'] # 设置字体
plt.rcParams['axes.unicode_minus'] = False # plt.rcParams['axes.unicode_minus']是一个配置参数,决定了坐标轴上负号的显示方式
plt.xlabel('时间')
plt.ylabel('站点')
plt.title('列车运行图')
plt.legend()
plt.show()
可以借鉴下
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
import math
import pandas as pd
date = pd.date_range('2018-01-01','2018-01-01 23:59:59',freq='1min').strftime('%Y-%m-%d %H:%M:%S').tolist()
date = pd.Series(date)
mu = 840
def bell_curve(area, peak, base):
x = np.linspace(0, 1440, 1440)
sigma = area/(math.sqrt(2*math.pi)*peak)
pdf_value = area*(stats.norm.pdf(x, mu, sigma))
plt.plot(x, area*(stats.norm.pdf(x, mu, sigma)))
plt.show()
return pdf_value
peak = int(input('Enter the peak value:'))
area = int(input('Enter the area value:'))
base = int(input('Enter the base value:'))
pdf = bell_curve(area, peak, base)
参考试试这个
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates
stations_up = ['北京', '北京', '天津', '天津', '石家庄', '石家庄', '邯郸', '邯郸']
stations_down = ['邯郸', '邯郸', '石家庄', '石家庄', '天津', '天津', '北京', '北京']
trains_up = [
{'schedule': [(0, '08:00:00'), (1, '08:00:30'), (2, '08:01:10'), (3, '08:01:50'),
(4, '08:02:50'), (5, '08:03:10'), (6, '08:04:50'), (7, '08:05:50')]},
{'schedule': [(0, '08:01:00'), (1, '08:01:30'), (2, '08:02:10'), (3, '08:02:50'),
(4, '08:03:50'), (5, '08:04:10'), (6, '08:05:50'), (7, '08:06:50')]},
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'), (3, '08:03:50'),
(4, '08:04:50'), (5, '08:05:10'), (6, '08:06:50'), (7, '08:07:50')]}
]
trains_down = [
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'), (3, '08:03:50'),
(4, '08:04:50'), (5, '08:05:10'), (6, '08:06:50'), (7, '08:07:50')]}
]
# 对于列车上行
for t in trains_up:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
t['schedule'][i] = (t['schedule'][i][0], dt)
for t in trains_up:
times = [x[1] for x in t['schedule']]
plt.plot(times, stations_up, linewidth=1)
# 对于下行
for t in trains_down:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
t['schedule'][i] = (t['schedule'][i][0], dt)
for t in trains_down:
times = [x[1] for x in t['schedule']]
plt.plot(times, stations_down, linewidth=1)
# 横纵轴的基本设置
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S")) # 设置x轴日期格式
plt.tick_params(axis="both", labelsize=10)
plt.gcf().autofmt_xdate() # 自动旋转日期标记
plt.rcParams['font.sans-serif'] = ['SimSun'] # 设置字体,
plt.rcParams['axes.unicode_minus'] = False # 设置负号显示方式
plt.xlabel('时间')
plt.ylabel('站点')
plt.title('列车运行图')
plt.legend()
plt.show()
要将x轴改为时间,可以使用matplotlib.dates模块中的日期处理器(date formatter)和定位器(date locator)来实现。首先,需要将横坐标的数据转换为datetime对象,然后在绘制图形时使用日期处理器和定位器来设置x轴标签的格式和间隔。
以下是修改过后的代码示例:
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates
stations_up = ['北京' ,'北京' ,'天津','天津', '石家庄', '石家庄', '邯郸','邯郸']
stations_down=[ '邯郸','邯郸', '石家庄', '石家庄','天津','天津','北京' ,'北京']
trains_up = [
{'schedule': [(0, '08:00:00'), (1, '08:00:30'), (2, '08:01:10'),(3, '08:01:50'),(4, '08:02:50'),(5, '08:03:10'),(6, '08:04:50'),(7, '08:05:50')]},
{'schedule': [(0,'08:01:00'), (1, '08:01:30'), (2, '08:02:10'),(3, '08:02:50'),(4, '08:03:50'),(5, '08:04:10'),(6, '08:05:50'),(7, '08:06:50')]},
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'),(3, '08:03:50'),(4, '08:04:50'),(5, '08:05:10'),(6, '08:06:50'),(7, '08:07:50')]}
]
trains_down=[
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'),(3, '08:03:50'),(4, '08:04:50'),(5, '08:05:10'),(6, '08:06:50'),(7, '08:07:50')]}
]
# 对于列车上行
for t in trains_up:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
# 将时间转换为datetime对象
t['schedule'][i] = (t['schedule'][i][0], dt)
# 绘制列车上行图形
for t in trains_up:
times = [x[1] for x in t['schedule']]
stations = [stations_up[x[0]] for x in t['schedule']]
plt.plot(times, stations, linewidth=1)
# 对于列车下行
for t in trains_down:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
# 将时间转换为datetime对象
t['schedule'][i] = (t['schedule'][i][0], dt)
# 绘制列车下行图形
for t in trains_down:
times = [x[1] for x in t['schedule']]
stations = [stations_down[x[0]] for x in t['schedule']]
plt.plot(times, stations, linewidth=1)
# 设置x轴的日期处理器和定位器
ax = plt.gca()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) # 设置日期处理器,以"%H:%M:%S"格式显示时间
ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=10)) # 设置日期定位器,每10分钟显示一个刻度
# 设置横纵轴的标签和标题
plt.xlabel('时间')
plt.ylabel('站点')
plt.title('列车运行图')
plt.legend()
# 显示图形
plt.show()
在以上代码中,我们使用datetime.datetime.strptime函数将时间字符串转换为datetime对象,然后将所有的时间数据都转换为datetime对象。在绘制图形时,我们将x轴的数据设置为datetime对象,然后使用mdates.DateFormatter和mdates.MinuteLocator来设置x轴标签的格式和间隔。最后,我们通过ax.xaxis.set_major_formatter和ax.xaxis.set_major_locator函数来设置日期处理器和定位器。
注意,如果数据中的时间跨度很大,比如几天或几个月,可以使用不同的日期定位器和处理器来显示更合适的刻度和标签。例如,可以使用mdates.HourLocator来定位每小时的刻度,使用mdates.DayLocator来定位每天的刻度,使用mdates.DateFormatter来显示日期。