简体   繁体   English

使用来自 csv 的 matplotlib 和 pandas 的 Python 图表不显示所有 x 轴标签

[英]Python chart using matplotlib and pandas from csv does not show all x-axis labels

I have to chart a data from csv somewhere from my directory.我必须在我的目录中的某处绘制来自 csv 的数据。 I am using python by learning some samples online.我通过在线学习一些示例来使用python。 Problem is, I can't find any solution to show all x-axis labels.问题是,我找不到任何解决方案来显示所有 x 轴标签。

x轴:日本、malay、sing、泰国、美国

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

pathcsv = r'D:\iPython\csvfile\samplecsv2.csv'
df = pd.read_csv(pathcsv)
df.set_index('Names').plot()

plt.show()

you can do that by using set_xticklabels to set the names and set_xticks to show ticks for each country.您可以通过使用set_xticklabels设置名称并使用set_xticks显示每个国家/地区的刻度来做到这一点。 Updated code is below...更新的代码如下...

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

pathcsv = r'D:\iPython\csvfile\samplecsv2.csv'
ax =df.set_index('Names').plot()
ax.set_xticks(np.arange(len(df))) #Show ticks for each country
ax.set_xticklabels(df.Names)      #Show labels as in df.Names
plt.show()

Output graph输出图

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM