matplotlib ax bar color 设置ax bar的颜色、 透明度、label legend

matplotlib ax bar color

设置ax bar的颜色

d = nx.degree(g1)
print("网络的度分布为:{}".format(d))

degree_sequence_g1 = sorted((d for n, d in g1.degree()), reverse=True)
degree_sequence_g2 = sorted((d for n, d in g2.degree()), reverse=True)
import numpy as np
fig, ax = plt.subplots()
ax.bar(*np.unique(degree_sequence_g1, return_counts=True),color='b',alpha=0.3,label='tomor')
ax.bar(*np.unique(degree_sequence_g2, return_counts=True),color='r',alpha=0.3,label='normal')
ax.set_ylim(ymin = 0, ymax = 450)
ax.set_title("Degree histogram")
ax.set_xlabel("Degree")
ax.set_ylabel("# of Nodes")
ax.legend(loc = "best")
plt.show()
print('plot Degree histogram graph finished!')

matplotlib.axes.Axes.bar — Matplotlib 3.5.2 documentation

### Matplotlib 颜色代码使用方法 在 Matplotlib 中,可以通过不同的方式来指定颜色。以下是几种常用的方法: #### 使用单字母缩写表示基础颜色 Matplotlib 支持通过单个字母来快速指定一些常见的颜色[^1]。 | 缩写 | 对应颜色 | | --- | -------- | | b | 蓝色 | | c | 青色 | | g | 绿色 | | k | 黑色 | ```python import matplotlib.pyplot as plt plt.plot([0, 1], [0, 1], 'b-', label='蓝色') plt.legend() plt.show() ``` #### 利用预定义的颜色名称 除了上述简单的字符外,还可以采用更直观的名字来描述色彩,比如`'darkred'`, `'lightgreen'`等[^3]。 ```python import numpy as np import matplotlib.pyplot as plt t = np.arange(0., 5., 0.2) plt.plot(t, t, 'r--', t, t ** 2, 'bs', t, t ** 3, 'g^', label=['线性', '平方', '立方']) plt.xlabel('时间 (s)') plt.ylabel('距离 (m)') plt.title('不同函数随时间变化图') plt.legend() plt.show() ``` 这里虽然没有直接展示深色的例子,但是可以从官方文档或其他资源获取更多具体的命名选项。 #### 应用十六进制字符串形式 对于精确控制色调的情况,则推荐利用HTML风格的六位数HEX值。 ```python fig, ax = plt.subplots(facecolor='#EAEAF2') # 设置背景为浅灰色 ax.set_facecolor('#F7F7F9') # 子图内部填充色设为稍亮一点的灰度 line = ax.plot(range(10), color="#FFAABB") # 绘制一条带有自定义RGB颜色线条 ``` #### RGB 或 RGBA 元组表达法 当需要微调透明度时,可借助RGBA四元数组;仅需RGB三者则构成不带透明效果的标准模式。 ```python from random import randint colors = [(randint(0, 255)/255, randint(0, 255)/255, randint(0, 255)/255) for _ in range(4)] for i, color in enumerate(colors): plt.bar(i, height=randint(1, 10), width=0.8, align="center", alpha=0.7, color=color) plt.xticks(range(len(colors)), ['条形{}'.format(x+1) for x in range(len(colors))]) plt.show() ``` 以上就是关于如何在Matplotlib项目里运用各种类型的色彩编码的一些实例说明。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值