How to change background color for scatter plot in matplotlib

旧城冷巷雨未停 提交于 2019-12-08 06:40:20

问题


How do I change the background color of a scatter plot in matplotlib?

Currently I have

import matplotlib.pyplot as plt
plt.scatter(X, Y, c=T, marker='o', s=(0.005*r), linewidth=0, cmap=cm.coolwarm)
plt.scatter(X_stars, Y_stars, marker='o', s=(0.00000005*r), color='white')

plt.savefig(filename, format='ps')

I want the background to be black, not white. I already changed facecolor and edgecolor to black, but without the desired effect. Setting transparent=True made it transparent so that I could change the background in Photoshop, but it must work in matplotlib as I have a very large number of plots.


回答1:


EDIT: As mentionned by endolith in the comments axisbg was deprecated in version 2.0 of matplotlib. Use facecolor instead.

ax = fig.add_subplot(111, facecolor='black')

You could use the axisbg argument of the add_subplot method. Here's a little example:

import matplotlib.pyplot as plt

a = random(100)*10
b = range(100)
fig = plt.figure(1)
ax = fig.add_subplot(111, axisbg='black')
ax.scatter(a,b)
fig.canvas.draw()


来源:https://stackoverflow.com/questions/17989654/how-to-change-background-color-for-scatter-plot-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!