如何使用文本作为ytick标签生成图 [英] How to produce plot with text as ytick labels

查看:111
本文介绍了如何使用文本作为ytick标签生成图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是了解Python库pandasmatplotlib.您能举例说明如何使用matplotlib生成与此图类似的图吗?

I am just getting to know the Python libraries pandas and matplotlib. Can you show me as an example how to produce a plot similar to this one with matplotlib:

在右侧y轴上列出了数据实例的名称.下面的x轴用于表示与每个实例相关的某些值.

On the y-axis to the right the names of the data instances are listed. The x-axis below is for some value related to each instance.

数据采用类似于以下格式的.csv格式:

The data is in .csv format similar to this:

name;value1;value2
uk-2007-05;0.01;1000

理想情况下,value1value2都应在同一图中绘制,并使用不同的颜色或标记.

Ideally, both value1 and value2 should be plotted in the same plot with different colors or markers.

推荐答案

import random
import matplotlib.pyplot as plt

labels = [chr(j) for j in range(97, 115)]

fake_data1 = [random.random() for l in labels]
fake_data2 = [random.random() for l in labels]
y_data = range(len(labels))

figure()
ax = gca()

ax.grid(True)
ax.scatter(fake_data1, y_data, color='r')
ax.scatter(fake_data2, y_data, color='b')

ax.set_yticks(range(len(labels)))
ax.set_yticklabels(labels)
ax.invert_xaxis()
plt.draw()

其中labels是标签列表,y_data是每个数据点的标签索引,而fake_data1fake_data2x值.

Where labels is a list of your labels, y_data is indices of the labels for each data point, and fake_data1 and fake_data2 are you x values.

这篇关于如何使用文本作为ytick标签生成图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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