python画图颜色有哪些_Python Matplotlib Boxplot颜色

1586010002-jmsa.png

I am trying to make two sets of box plots using Matplotlib. I want each set of box plot filled (and points and whiskers) in a different color. So basically there will be two colors on the plot

My code is below, would be great if you can help make these plots in color. d0 and d1 are each list of lists of data. I want the set of box plots made with data in d0 in one color, and the set of box plots with data in d1 in another color.

plt.boxplot(d0, widths = 0.1)

plt.boxplot(d1, widths = 0.1)

解决方案

To colorize the boxplot, you need to first use the patch_artist=True keyword to tell it that the boxes are patches and not just paths. Then you have two main options here:

set the color via ...props keyword argument, e.g.

boxprops=dict(facecolor="red"). For all keyword arguments, refer to the documentation

Use the plt.setp(item, properties) functionality to set the properties of the boxes, whiskers, fliers, medians, caps.

obtain the individual items of the boxes from the returned dictionary and use item.set_(...) on them individually. This option is detailed in an answer to the following question: python matplotlib filled boxplots, where it allows to change the color of the individual boxes separately.

The complete example, showing options 1 and 2:

import matplotlib.pyplot as plt

import numpy as np

data = np.random.normal(0.1, size=(100,6))

data[76:79,:] = np.ones((3,6))+0.2

plt.figure(figsize=(4,3))

# option 1, specify props dictionaries

c = "red"

plt.boxplot(data[:,:3], positions=[1,2,3], notch=True, patch_artist=True,

boxprops=dict(facecolor=c, color=c),

capprops=dict(color=c),

whiskerprops=dict(color=c),

flierprops=dict(color=c, markeredgecolor=c),

medianprops=dict(color=c),

)

# option 2, set all colors individually

c2 = "purple"

box1 = plt.boxplot(data[:,::-2]+1, positions=[1.5,2.5,3.5], notch=True, patch_artist=True)

for item in ['boxes', 'whiskers', 'fliers', 'medians', 'caps']:

plt.setp(box1[item], color=c2)

plt.setp(box1["boxes"], facecolor=c2)

plt.setp(box1["fliers"], markeredgecolor=c2)

plt.xlim(0.5,4)

plt.xticks([1,2,3], [1,2,3])

plt.show()

xgyF3.png

好的,关于您的问题,我可以回答您的第二个问题,并提供一些有关您第一个问题的信息。 关于Python画图函数大全,以下是一些常用的Python画图函数及其用途: 1. matplotlib.pyplot.plot():绘制线条图 2. matplotlib.pyplot.scatter():绘制散点图 3. matplotlib.pyplot.bar():绘制柱状图 4. matplotlib.pyplot.hist():绘制直方图 5. matplotlib.pyplot.boxplot():绘制箱线图 6. matplotlib.pyplot.imshow():绘制图像 7. seaborn.lineplot():绘制线条图 8. seaborn.scatterplot():绘制散点图 9. seaborn.barplot():绘制柱状图 10. seaborn.histplot():绘制直方图 11. seaborn.boxplot():绘制箱线图 12. seaborn.heatmap():绘制热力图 关于您的第一个问题,如果您想用Python每隔一段时间画折线图,您可以使用matplotlib库中的animation模块。以下是一个简单的示例代码: ``` import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation # 生成随机数据 x_data = np.arange(0, 10, 0.1) y_data = np.sin(x_data) # 初始化图像 fig, ax = plt.subplots() line, = ax.plot(x_data, y_data) # 更新函数 def update(frame): y_data = np.sin(x_data + frame * 0.1) line.set_ydata(y_data) return line, # 动画函数 ani = FuncAnimation(fig, update, frames=100, interval=50, blit=True) # 显示动画 plt.show() ``` 这个示例代码会每隔50毫秒更新一次折线图,总共更新100次,您可以根据需要调整更新的时间间隔和更新的次数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值