简体   繁体   English

python-如何使用重复的x轴绘制数据

[英]python - How to plot data with repeating x axis

I would like to plot day dependent data over many years where years should be on the x-axis (2016,2017,2018 for example). 我想绘制多年应该依赖于天的数据,其中年份应该在x轴上(例如2016、2017、2018)。 What is a good approach to do this? 有什么好的方法可以做到这一点?

For every year I have a list of days which I would plot on the x-axis but of course python keeps this axis and plots all the data of different years above each other. 对于每一年,我都有一份要在x轴上绘制的天数列表,但是python保留了该轴,并绘制了彼此不同年份的所有数据。

Any suggestions? 有什么建议么?

Code: 码:

A shortened version of my dictionary L_B_1_mean looks like this: 我的字典L_B_1_mean看起来像这样:

2016018 5.68701407589
2016002 4.72437644462
2017018 3.39389424822
2018034 7.01093439059
2018002 8.79958946488
2017002 3.55897852367

the code: 编码:

data_plot = {"x":[], "y":[], "label":[]}
for label, coord in L_B_1_mean.items():
    data_plot["x"].append(int(label[-3:]))             
    data_plot["y"].append(coord)
    data_plot["label"].append(label)


# add labels
for label, x, y in zip(data_plot["label"], data_plot["x"], data_plot["y"]):
    axes[1].annotate(label, xy = (x, y+0.02), ha= "left")


# 1 channel different years Plot
plt_data = axes[1].scatter(data_plot["x"], data_plot["y"])

I construct my x-values here: data_plot["x"].append(int(label[-3:])) where I read the name tag like: 2016002 and get only the day value: 002 我在这里构造我的x值: data_plot["x"].append(int(label[-3:]))在这里读取名称标签,如:2016002,仅获取日值:002

In the end I have 365 days per year and now i would like to plot the data of 2016 then 2017 and then 2018 all after each other instead of on top of each other 最终,我每年有365天,现在我想绘制2016年,2017年和2018年的数据,彼此之后而不是彼此叠加

You have a dict 你有一个命令

L_B_1_mean 

{'2016018': 5.68701407589,
 '2016002': 4.72437644462,
 '2017018': 3.39389424822,
 '2018034': 7.010934390589999,
 '2018002': 8.79958946488,
 '2017002': 3.55897852367}

plot using pandas: 使用熊猫剧情:

import pandas as pd

You can simply create a pandas series from this dict: 您可以根据以下命令简单地创建一个熊猫系列:

s = pd.Series(L_B_1_mean)

2016018    5.687014
2016002    4.724376
2017018    3.393894
2018034    7.010934
2018002    8.799589
2017002    3.558979
dtype: float64

...and cast the strings in the index to dates: ...并将索引中的字符串转换为日期:

s.index = pd.to_datetime(s.index, format='%Y%j')

2016-01-18    5.687014
2016-01-02    4.724376
2017-01-18    3.393894
2018-02-03    7.010934
2018-01-02    8.799589
2017-01-02    3.558979
dtype: float64

Then you can plot your data easily: 然后,您可以轻松地绘制数据:

s.plot(marker='o')

在此处输入图片说明

plot using datetime and matplotlib: 使用datetime和matplotlib进行绘图:

import datetime as DT
import matplotlib.pyplot as plt

t = [DT.datetime.strptime(k, '%Y%j') for k in L_B_1_mean.keys()]
v = list(L_B_1_mean.values())

v = sorted(v, key=lambda x: t[v.index(x)])
t = sorted(t)

plt.plot(t, v, 'b-o')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过在 matplotlib 中的 x 轴上重复字符串来制作 plot? - how to make a plot by repeating strings on x-axis in matplotlib? Python:针对X轴上的时间绘制数据 - Python: Plot data against time in X axis 如何在 Python 中以分钟为 plot x 轴? - How to plot x axis with minutes in Python? 如何在Python中将日期列绘制为x轴并在y轴上绘制数据 - How to plot date column as x-axis and data on y axis in Python 如何 plot x 轴上的几个数据progeress? - How to plot several data progeress on x axis? Python plot 散点数据在 X 轴上表现不佳 - Python plot scatter data don't behave well on X axis Python 在 x 轴上绘制多年数据的日​​时间 - Python Plot day time on x-axis for many years of data 如何在 Python Matplotlib 中的同一 x 轴上 plot 具有不同开始日期的多个时间序列数据? - How to plot multiple timeseries data with different start date on the same x-axis in Python Matplotlib? 如何使用Python绘制其数据在x轴上间距不相等的2D矩阵? - How to plot 2D matrix whose data is not equally spaced along x-axis using Python? 如图所示,x 轴为 plot 天,y 轴为时间,热图 plot 为 python 中的值? - How to plot day in x axis, time in y axis and a heatmap plot for the values in python as shown in the figure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM