如何绘制两种不同颜色的曲线? (不使用 pandas ) [英] How to plot one curve in two different colors? (NOT using pandas)

查看:100
本文介绍了如何绘制两种不同颜色的曲线? (不使用 pandas )的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用matplotlib.pyplot创建一个图,该图显示地球与火星之间的距离随时间的变化.除了那几个月3月至8月应以与其他月份不同的颜色显示. 从包含日期,距离和标志的数组中提供数据,该标志指示日期是否在3月到8月之间.

I need to create a plot using matplotlib.pyplotthat shows distance between Earth and Mars over time. In addition to that some months e.g. March to August should be shown in a different color than the other months. Data is provided from an array containing the date, distance and also a flag indicating whether the date is in the March to August-span or not.

包含全部数据的数组称为master_array.第一列包含日期,第二列包含距离. s/w标志位列第七. (夏季为's',冬季为'w').

The array containing the whole data is called master_array. First column containing the date, second the distance; seventh the s/w-flag. ('s' for summer, 'w' for winter).

我试图利用这样的事实,即pyplot.plot为每个.plot命令切换颜色,这种方式我首先绘制了冬季,然后又绘制了夏季.

I tried to make use of the fact that pyplot.plot switches color for every single .plot command in a way that I first plot the winter months and in a second plot the summer months.

import numpy as np
from matplotlib import pyplot as plt

def md_plot2(dt64=np.array, md=np.array):
    """Erzeugt Plot der Marsdistanz (y-Achse) zur Zeit (x-Achse)."""
    plt.style.use('seaborn-whitegrid')

    y, m, d = dt64.astype(int) // np.c_[[10000, 100, 1]] % np.c_[[10000, 100, 100]]
    dt64 = y.astype('U4').astype('M8') + (m-1).astype('m8[M]') + (d-1).astype('m8[D]')

    wFilter = np.argwhere(master_array[:,6] == 0)
    sFilter = np.argwhere(master_array[:,6] == 1)

    plt.plot(dt64[wFilter], md[wFilter], label='Halbjahr der fallenden \nTemperaturen')
    plt.plot(dt64[sFilter], md[sFilter], label='Halbjahr der steigenden \nTemperaturen')

    plt.xlabel("Zeit in Jahren\n")
    plt.xticks(rotation = 45)
    plt.ylabel("Marsdistanz in AE\n(1 AE = 149.597.870,7 km)")
    plt.legend(loc='upper right', frameon=True)

plt.figure('global betrachtet...') # diesen Block ggf. auskommentieren
#plt.style.use('seaborn-whitegrid')
md_plot2(master_array[:,0], master_array[:,1]) # Graph

plt.show()
#plt.close()

现在的问题是,在夏季的最后一点和下一个夏季的第一点之间,夏季图显示一条线,而其他图(对于冬季)则显示介于两者之间的正确的冬季数据两个夏天. 数据包含许多数据点,这些数据点将导致这两种颜色的许多段. 当下一个数据点超过将来的一天时,如何停止绘制直线? pyplot可能有另一种方法可以完成此任务吗?

Problem now is that between the last point of a summer period and first point of the following summer period the plot for summers shows a line where the other plot (for winters) shows the correct data of the winter period that lays between those two summers. The data is containing many data points what will lead to many segments of those two colors. How can I stop the plot to draw a line, when the next data point is more that one day in the future? Is there maybe another method of pyplot for this task?

由于我在脚本中编写了很多代码而没有使用熊猫,所以我很高兴找到不使用熊猫的解决方案,因为我不想避免使用它,因为我不想I肿我的脚本当有一种方法可以通过使用我已经在使用的模块来完成任务时.我还阅读了它会降低速度的方法,此处.

As I wrote a lot of code in the script where this belongs without using pandas I would be very happy to find a solution to this problem not using pandas as I try to avoid it because I don't want to bloat my script when there is a way to get the task done by using the modules I am already using. I also read it would decrease the speed here.

推荐答案

multicolored_line 示例看起来像您想要的.

The multicolored_line example looks like what you're going for.

另请参阅这篇文章,以获取有关Matplotlib中的颜色图系统的更多信息

Also see this article for more information about the colormap system in Matplotlib.

这篇关于如何绘制两种不同颜色的曲线? (不使用 pandas )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆